views:

935

answers:

7

In PHP, I want to read a file into a variable and process the PHP in the file at the same time without using output buffering. Is this possible?

Essentially I want to be able to accomplish this without using ob_start():

<?php
ob_start();
include 'myfile.php';
$xhtml = ob_get_clean();
?>

Is this possible in PHP?

Update: I want to do some more complex things within an output callback (where output buffering is not allowed).

+7  A: 

From what I can tell in the PHP documentation, no. Why do you want to avoid output buffering?

The only way to get around this would be hacky methods involving either invoking the command line php client or doing a curl request based on what's available and what the particular requirements are.

KernelM
That's kinda what I was afraid of :( Thanks!
Joe Lencioni
A: 

Do a curl request to the php page, essentially pretending to be the browser.

Joeri Sebrechts
A: 

What you could do, if the file is local, is load the script into a variable as a string, then run eval on the string. Then you can do all your other stuff afterwards. Otherwise, you have to use output buffering.

The Wicked Flea
Unfortunately, that won't work because eval() evaluates PHP code whereas accessing a page will only evaluate the PHP code if it is in a PHP code block (e.g. between <?php and ?>)
Joe Lencioni
+1  A: 

Joeri Sebrechts is correct. An equivalent and slightly easier method is available if the PHP script is HTTP accessible:

$data = file_get_contents('http://google.com/');

It should be noted that using output buffering would be easier on resources.

Many servers disable allow_furl_open and this will not be an option.
dcousineau
A: 

$fileData = file_get_contents('fileOnDisk.php'); $results = eval($fileData);

But check the documentation on eval, because you actually have to have the file you are calling return its results rather than just echo them:

http://us2.php.net/eval

Zak
+1  A: 

After reading everybody's suggestions, reading a bunch of documentation, and playing around with some things, I came up with this:

<?php
$file = file_get_contents('/path/to/file.php');
$xhtml = eval("?>$file");
?>

It's as close as I could get but it unfortunately doesn't work. The key to this is to include the closing PHP bit (?>) before the contents of the file. This will take the eval() out of PHP-evaluation mode and will treat the contents of the file starting as non-PHP code. Then if there are PHP code blocks within the file, those will be evaluated as PHP. The bummer is that it doesn't save the eval'd content in the variable, it just outputs it to the page.

Thanks for the help everybody!

Joe Lencioni
And that actually returns the output of the eval'd file? From what I read at http://us.php.net/manual/en/function.eval.php you'd still need to use output buffering.
KernelM
Dang, I guess you're right. :(
Joe Lencioni
+2  A: 

A little known feature of PHP is being able to treat an included/required file like a function call, with a return value.

For example:

// myinclude.php
$value = 'foo';
$otherValue = 'bar';
return $value . $otherValue;


// index.php
$output = include './myinclude.php';
echo $output;
// Will echo foobar
Wesley Mason