23

I tried:

$test = include 'test.php';

But that just included the file normally

Ryan
  • 461
  • 3
  • 6
  • 15

6 Answers6

35

You'll want to look at the output buffering functions.

//get anything that's in the output buffer, and empty the buffer
$oldContent = ob_get_clean();

//start buffering again
ob_start();

//include file, capturing output into the output buffer
include "test.php";

//get current output buffer (output from test.php)
$myContent = ob_get_clean();

//start output buffering again.
ob_start();

//put the old contents of the output buffer back
echo $oldContent;

EDIT:

As Jeremy points out, output buffers stack. So you could theoretically just do something like:

<?PHP
function return_output($file){
    ob_start();
    include $file;
    return ob_get_clean();
}
$content = return_output('some/file.php');

This should be equivalent to my more verbose original solution.

But I haven't bothered to test this one.

Jeremy Roman
  • 16,137
  • 1
  • 43
  • 44
timdev
  • 61,857
  • 6
  • 82
  • 92
11

Try something like:

ob_start();
include('test.php');
$content = ob_get_clean();
Brandon
  • 68,708
  • 30
  • 194
  • 223
8

Try file_get_contents().

This function is similar to file(), except that file_get_contents() returns the file in a string.

Florent
  • 12,310
  • 10
  • 49
  • 58
antpaw
  • 15,444
  • 11
  • 59
  • 88
5

Solution #1: Make use of include (works like a function): [My best solution]

File index.php:

<?php
$bar = 'BAR';
$php_file = include 'included.php';
print $php_file;
?>

File included.php:

<?php
$foo = 'FOO';
return $foo.' '.$bar;
?>
<p>test HTML</p>

This will output FOO BAR, but Note: Works like a function, so RETURN passes contents back to variable (<p>test HTML</p> will be lost in the above)


Solution #2: op_buffer():

File index.php:

<?php
$bar = 'BAR';
ob_start();
include 'included.php';

$test_file = ob_get_clean(); //note on ob_get_contents below
print $test_file;
?>

File included.php:

<?php
$foo = 'FOO';
print $foo.' '.$bar;
?>
<p>test HTML</p>

If you use ob_get_contents() it will output FOO BAR<p>test HTML</p> TWICE, make sure you use ob_get_clean()


Solution #3: file_get_contents():

File index.php:

<?php
$bar = 'BAR';
$test_file = eval(file_get_contents('included.php'));

print $test_file;
?>

File included.php:

$foo = 'FOO';
print $foo.' '.$bar;

This will output FOO BAR, but Note: Include.php should not have <?php opening and closing tags as you are running it through eval()

Duncanmoo
  • 3,535
  • 2
  • 31
  • 32
  • 1
    The only reason why the buffer would output twice is if the buffer was not held and then cleaned before the end of file, meaning the buffer is automatically echoed. Perhaps that's the issue you've been having. – worldofjr Sep 25 '14 at 05:25
  • 1
    Aah, thanks @worldofjr, you were not really specific but pointed me in the right direction on OB; `ob_get_contents()` should be `ob_get_clean()` otherwise it outputs twice. Answer being edited accordingly. – Duncanmoo Sep 26 '14 at 20:21
  • 1. Is solution #3 better? 2. In solution #2, can you use a variable for the file name or does the file name (e.g., `'included.php'`) have to be hard-coded? 3. What if the argument passed to `eval` contains a mix of html and embedded php tags, will it throw an error? – toddmo May 05 '18 at 17:50
  • @toddmo I think option #3 is the worst, I would not want files in my webroot which then called directly through the browser to render actual PHP code as text. IMO all PHP should be `` enclosed. – Duncanmoo May 07 '18 at 07:08
  • @Duncanmoo, I had to go with option number 2. `eval` doesn't allow mixed php and html. And, to answer my own question #2: yes, a variable can be used after include, to give the file name dynamically. – toddmo May 08 '18 at 02:50
0

The other answers, for reasons unknown to me, don't quite reach the correct solution.

I suggest using the buffer, but you have to get the contents and then clean the buffer before the end of the page, otherwise it is outputted. Should you wish to use the output from the included file, you should use op_get_contents(), which will return a string of the contents of the buffer.

You also don't need to loop over the includes as each will just add to the buffer (unless you clean it first).

You therefore could use the following;

ob_start();
include_once('test.php');
include_once('test2.php');
$contents = ob_get_contents();
ob_end_clean();

Hope this helps.

worldofjr
  • 3,868
  • 8
  • 37
  • 49
-1

You can use the function file_get_contents.

Matt
  • 74,352
  • 26
  • 153
  • 180
assaqqaf
  • 1,575
  • 4
  • 21
  • 38