我想要求/包含一个文件并将其内容检索到一个变量中。
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;
?>
类似于 file_get_contents()
但它仍应执行 PHP 代码。
这可能吗?
最佳答案
如果您包含的文件返回了一个变量...
include.php
<?php
return 'abc';
...然后您可以将它分配给这样的变量...
$abc = include 'include.php';
否则,使用输出缓冲。
ob_start();
include 'include.php';
$buffer = ob_get_clean();
关于php - 要求/包含到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5948395/