我想將一個文件的內容加載到一個字符串中。這個文件包含一些php代碼,需要一些變量才能正常工作。將多個文件內容加載(使用變量)到字符串中的最佳方式是什麼?
什麼是最有效的方法來實現這一目標?
以下是我認爲將是很好的做到這一點的方法:
與會話變量:
for ($i = 0; $i < 10; $i++)
{
$_SESSION[$key] = $i;
$content .= file_get_contents($fileName);
}
然後,我可以從加載文件訪問變量。
使用GET方法:
for ($i = 0; $i < 10; $i++)
{
$content .= file_get_contents($fileName."?key=$i");
}
使用post方法:
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
請解釋你想要完成什麼,輸入/輸出 – Dani
什麼是10倍循環? – Phil
@這是一個隨機數,我只是想表明我想多次加載文件。 –