Php 将多个文件内容(使用变量)加载到字符串中的最佳方式是什么?
我想将文件的内容加载到字符串中。该文件包含一些php代码,需要一些变量才能正常工作 实现这一目标最有效的方法是什么 以下是我认为最好的方法: 使用会话变量:Php 将多个文件内容(使用变量)加载到字符串中的最佳方式是什么?,php,Php,我想将文件的内容加载到字符串中。该文件包含一些php代码,需要一些变量才能正常工作 实现这一目标最有效的方法是什么 以下是我认为最好的方法: 使用会话变量: for ($i = 0; $i < 10; $i++) { $_SESSION[$key] = $i; $content .= file_get_contents($fileName); } 听起来你想使用输出缓冲,例如 $content = ''; for ($i = 0; $i < 10; $i++) {
for ($i = 0; $i < 10; $i++)
{
$_SESSION[$key] = $i;
$content .= file_get_contents($fileName);
}
听起来你想使用输出缓冲,例如
$content = '';
for ($i = 0; $i < 10; $i++) {
ob_start();
include $fileName;
$content .= ob_get_contents();
ob_end_clean();
}
请解释你想要完成什么,输入/output@Phil这是一个随机数,我只是想表明我想多次加载该文件。但是您的$foo变量是如何传递到文件.php的?@JeanPhilippe我已经更新了我的答案,以便更好地匹配您问题的代码。当您
包含
一个文件时,它会继承调用作用域输出缓冲(ob
)是实现这一点的最佳方法,这是一个很好的答案
for ($i = 0; $i < 10; $i++)
{
$postData = http_build_query($i);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postData
)
);
$context = stream_context_create($opts);
$content .= file_get_contents($fileName, false, $context);
}
<?php echo $_GET['key']; /*(or $_POST or $_SESSION)*/ ?>
0
1
2
3
4
5
6
7
8
9
$content = '';
for ($i = 0; $i < 10; $i++) {
ob_start();
include $fileName;
$content .= ob_get_contents();
ob_end_clean();
}
echo $i, PHP_EOL;