Get the contents of a php file after its launch / execution

How can I put the result of inclusion in a PHP variable?

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

+4
source share
2 answers

Either capture everything printed in the include file through output buffering

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

or, conversely, set the return value in a script, for example

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

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

+7
source

or simply return the value from the included file, as described here .

 return.php: <?php $var = 'PHP'; return $var; ?> $foo = include 'return.php'; echo $foo; // prints 'PHP' 
+3
source

Source: https://habr.com/ru/post/1306784/


All Articles