Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

file

php

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

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

like image 554
Jonathan. Avatar asked Dec 29 '22 20:12

Jonathan.


1 Answers

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

like image 154
Gordon Avatar answered Jan 04 '23 15:01

Gordon