php 要求/包含到变量中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5948395/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:59:52  来源:igfitidea点击:

require/include into variable

php

提问by user746379

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

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

就像file_get_contents()但比它应该仍然执行PHP代码。这可能吗?

回答by alex

If your included file returned a variable...

如果您包含的文件返回了一个变量...

include.php

包含.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();

回答by user6

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);
?>

回答by Tadeck

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

回答by Evgeniy Skulditsky

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.

我想eval(file_get_contents('include.php'))对你有帮助。请记住,可以在您的主机上禁用其他执行方式,如 shell_exec。

回答by dr.dimitru

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

仅当需要或包含的 php 文件返回某些内容(数组、对象、字符串、整数、变量等)时才有可能

$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 文件并且你想评估这个文件的内容,你可以:

<?php

function get_file($path){

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

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

回答by álvaro González

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:

在 PHP/7 中,您可以使用自调用匿名函数来完成简单的封装并防止全局范围被随机全局变量污染:

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:

PHP/5.3+ 的另一种语法是:

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

回答by tomslou

Or maybe something like this

或者可能是这样的

in file include.php:

在文件 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 文件中(这个文件的内容无关紧要):

<?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.

关键是,包含文件的内容与我提供的示例中的函数内容具有相同的范围。

回答by ankitjaininfo

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

使用shell_exec("php test.php"). 它返回执行的输出。

回答by JohnP

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.

require/include 不返回文件的内容。您必须进行单独的调用才能实现您想要做的事情。

EDIT

编辑

Using echowill 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

使用echo不会让你做你想做的。但是返回文件的内容将按照手册中的说明完成工作 - http://www.php.net/manual/en/function.include.php