require/include into variable
Solution 1:
If your included file returned a variable...
include.php
<?php
return 'abc';
...then you can assign it to a variable like so...
$abc = include 'include.php';
Otherwise, use output buffering.
ob_start();
include 'include.php';
$buffer = ob_get_clean();
Solution 2:
I've also had this issue once, try something like
<?php
function requireToVar($file){
ob_start();
require($file);
return ob_get_clean();
}
$test=requireToVar($test);
?>
Solution 3:
You can write in the included file:
<?php
return 'seconds etc.';
And in the file from which you are including:
<?php
$text = include('file.php'); // just assigns value returned in file
Solution 4:
In PHP/7 you can use a self-invoking anonymous function to accomplish simple encapsulation and prevent global scope from polluting with random global variables:
return (function () {
// Local variables (not exported)
$current_time = time();
$reference_time = '01-01-1970 00:00';
return "seconds passed since $reference_time GMT is $current_time";
})();
An alternative syntax for PHP/5.3+ would be:
return call_user_func(function(){
// Local variables (not exported)
$current_time = time();
$reference_time = '01-01-1970 00:00';
return "seconds passed since $reference_time GMT is $current_time";
});
You can then choose the variable name as usual:
$banner = require 'test.php';