类似于 file_get_contents() 但它仍应执行 PHP 代码。这可能吗-6ren">
gpt4 book ai didi

php - 要求/包含到变量中

转载 作者:IT王子 更新时间:2023-10-29 00:42:18 32 4
gpt4 key购买 nike

我想要求/包含一个文件并将其内容检索到一个变量中。

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/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com