开发者

Get contents of php file after it's ran/executed

开发者 https://www.devze.com 2022-12-26 10:06 出处:网络
How can I put the result of an include into a PHP variable?开发者_Python百科 I tried file_get_contents but it gave me the actual PHP code, whereas I want whats echoed.Either capture anything that\'s

How can I put the result of an include into a PHP variable?

开发者_Python百科

I tried file_get_contents but it gave me the actual PHP code, whereas I want whats echoed.


Either capture anything that's printed in the include file through output buffering

ob_start();
include 'yourFile.php';
$out = ob_get_contents();
ob_end_clean();

or alternatively, set a return value in the script, e.g.

// included script
return 'foo';
// somewhere else
$foo = include 'yourFile.php';

See Example 5 of http://de2.php.net/manual/en/function.include.php


or simply return a value from an included file as explained here.

return.php:
<?php

$var = 'PHP';

return $var;

?>


$foo = include 'return.php';

echo $foo; // prints 'PHP'
0

精彩评论

暂无评论...
验证码 换一张
取 消