23

I tried:

$test = include 'test.php';

But that just included the file normally

2
  • 4
    Can you be more verbose? Commented Jan 27, 2010 at 20:59
  • 1
    What do you want to achieve? That the content of test.php is stored in $test? Commented Jan 27, 2010 at 21:00

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Why is it necessary to stop any existing buffers? Output buffers in PHP stack: php.net/ob_start
It isn't -- I just hadn't noticed the stackability. Editing my answer.
11

Try something like:

ob_start();
include('test.php');
$content = ob_get_clean();

Comments

8

Try file_get_contents().

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

1 Comment

Definitely the best answer here. Short. Smart.
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()

5 Comments

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.
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.
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 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 <?php ?> enclosed.
@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.
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.

Comments

-1

You can use the function file_get_contents.

2 Comments

I wonder why you people would suggest output buffering when this is so much simpler.
Perhaps because file_get_contents doesn't execute the PHP code in the file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.