php - Including into a variable -
you know how print_r
accepts optional 2nd parameter if set true
return result instead of printing it?
i wish include
accepted 2nd parameter work same way.
it doesn't available alternatives? how can include file variable?
ob_start()
, ob_get_clean()
you:
ob_start(); include "file.php"; $result = ob_get_clean();
after ob_start() echoed captured, , ob_get_clean() used retrieve captured data.
you can include2
function this:
function include2($file) { ob_start(); include $file; return ob_get_clean(); }
and use this:
include2("file.php"); // return printed values instead of printing them
as noted @ircmaxell, include2
function not behave same include
since scope of include changes (from global function's scope). potentially break things if rely on global scope.
Comments
Post a Comment