Assigning contents to a variable with include/require_once

Alex picture Alex · Oct 16, 2011 · Viewed 9.6k times · Source

Is it possible to do like

$var = require_once('lol.php');

so that any HTML output that lol.php does will go inside $var?

I know about output buffering, but is there some special built-in function that already does this?

Answer

NikiC picture NikiC · Oct 16, 2011

$var = require_once('lol.php'); will only put the return value of the file into $var. If you don't return anything from it, it'll just be null.

If you want the output you will need to use output buffering:

ob_start();
require_once('lol.php');
$var = ob_get_clean();