Re: [Fwd: Re: [PHP] Re: How to take output from an include, and embed it into a variable?]
| From: | Robert Cummings | Date: | Sat, 26 Sep 2009 05:35:05 +0000 |
| Subject: | Re: [Fwd: Re: [PHP] Re: How to take output from an include, and embed it into a variable?] | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-298361@lists.php.net to get a copy of this message | ||
Carl Furst wrote:
You mean like this ? <? $file string = file_get_contents(urlencode($file_path)); <http://us3.php.net/manual/en/function.file-get-contents.php> $result = eval($file_string); ?> Well I see a few problems with this. One is that scope is not lexical. In other words if $foo exists somewhere in the script and $foo also exists in $file_string then $foo will retain the value it was set to in $file_string. That could lead to some debugging hell. Also, you would have to collect the output and manually return it which means you would have to keep an output cache which means you could only use scripts that cached output and returned them explicitly. However, the flip side is you could have a buffer declared in the local scope that collects the output of $file_string and then put that in the message, but that is not the same as: $foo = include $bar; # this is, of course, impossibleIf you want the include to have it's own variable scope then wrap it with a function. If you want to assign the resulting value via a single call then wrap it in a function. Fortunately both of these combined require one thing... wrap it in a function. <?php
function eval_include( $path )
{
ob_start();
include( $path );
$result = ob_get_contents();
ob_end_clean();
return $result;
}
$foo = eval_include( $bar );?> Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP