require/include into variable

Php

Php Problem Overview


I want to require/include a file and retrieve its contents into a variable.

test.php

<?php
echo "seconds passed since 01-01-1970 00:00 GMT is ".time();
?>

index.php

<?php
$test=require("test.php");
echo "the content of test.php is:<hr>".$test;
?>

Like file_get_contents() but than it should still execute the PHP code. Is this possible?

Php Solutions


Solution 1 - Php

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 - Php

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 - Php

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 - Php

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';

Solution 5 - Php

Use shell_exec("php test.php"). It returns the output of the execution.

Solution 6 - Php

require/include does not return the contents of the file. You'll have to make separate calls to achieve what you're trying to do.

EDIT

Using echo will not let you do what you want. But returning the contents of the file will get the job done as stated in the manual - http://www.php.net/manual/en/function.include.php

Solution 7 - Php

I think eval(file_get_contents('include.php')) help you. Remember that other way to execute like shell_exec could be disabled on your hosting.

Solution 8 - Php

It is possible only if required or included php file returns something (array, object, string, int, variable, etc.)

$var = require '/dir/file.php';

But if it isn't php file and you would like to eval contents of this file, you can:

<?php

function get_file($path){

	return eval(trim(str_replace(array('<?php', '?>'), '', file_get_contents($path))));
}

$var = get_file('/dir/file.php');

Solution 9 - Php

Or maybe something like this

in file include.php:

<?php
  //include.php file
  $return .= "value1 ";
  $return .= time();

in some other php file (doen't matter what is a content of this file):

<?php
  // other.php file
  function() {
    $return = "Values are: ";
    include "path_to_file/include.php";

    return $return;
  }

return will be look like this for example:

Values are: value1, 145635165

The point is, that the content of included file has the same scope as a content of function in example i have provided about.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser746379View Question on Stackoverflow
Solution 1 - PhpalexView Answer on Stackoverflow
Solution 2 - Phpuser6View Answer on Stackoverflow
Solution 3 - PhpTadeckView Answer on Stackoverflow
Solution 4 - PhpÁlvaro GonzálezView Answer on Stackoverflow
Solution 5 - PhpankitjaininfoView Answer on Stackoverflow
Solution 6 - PhpJohnPView Answer on Stackoverflow
Solution 7 - PhpEvgeniy SkulditskyView Answer on Stackoverflow
Solution 8 - Phpdr.dimitruView Answer on Stackoverflow
Solution 9 - PhptomslouView Answer on Stackoverflow