我想要/包含一个文件并将其内容检索到一个变量中.
test.php的
<?php
echo "seconds passed since 01-01-1970 00:00 GMT is ".time();
?>
Run Code Online (Sandbox Code Playgroud)
的index.php
<?php
$test=require("test.php");
echo "the content of test.php is:<hr>".$test;
?>
Run Code Online (Sandbox Code Playgroud)
喜欢file_get_contents()
但它应该仍然执行PHP代码.这可能吗?
use*_*er6 70
我曾经有过这个问题,尝试类似的东西
<?php
function requireToVar($file){
ob_start();
require($file);
return ob_get_clean();
}
$test=requireToVar($test);
?>
Run Code Online (Sandbox Code Playgroud)
ale*_*lex 70
如果您的包含文件返回了变量...
<?php
return 'abc';
Run Code Online (Sandbox Code Playgroud)
...然后你可以将它分配给一个变量......
$abc = include 'include.php';
Run Code Online (Sandbox Code Playgroud)
否则,使用输出缓冲.
ob_start();
include 'include.php';
$buffer = ob_get_clean();
Run Code Online (Sandbox Code Playgroud)
您可以在包含的文件中写入:
<?php
return 'seconds etc.';
Run Code Online (Sandbox Code Playgroud)
在您包含的文件中:
<?php
$text = include('file.php'); // just assigns value returned in file
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
46492 次 |
最近记录: |