有沒有辦法讓這成爲可能?PHP:將變量傳遞給file_get_contents()
// file.php
echo $foo;
然後在
// test.php
$foo = 'bar';
echo file_get_contents('file.php');
test.php的的輸出應爲 「欄」。
有沒有辦法讓這成爲可能?PHP:將變量傳遞給file_get_contents()
// file.php
echo $foo;
然後在
// test.php
$foo = 'bar';
echo file_get_contents('file.php');
test.php的的輸出應爲 「欄」。
我用:
$foo = 'bar';
require_once('./file.php');
而且在file.php它只是:
echo $foo;
ob_start();
include 'file.php';
echo ob_get_clean();
但實際上,這似乎是一個相當無意義的事情。如果您只想file.php
評估並且輸出它的內容,只需include
該文件。
是,使用OB-功能似乎在這種情況下是無意義的。這應該避免,因爲它不需要提供預期的輸出。 –
我會做這樣的:
// file.php
function outputFoo($foo) {
echo $foo;
}
// test.php
$foo = 'bar';
include 'file.php';
outputFoo($foo);
但實際上,所有的實際需要你的情況是:
// file.php
echo $foo;
// test.php
$foo = 'bar';
include 'file.php';
由於file.php將有你的$foo
-variable訪問以及。 (這隻適用於file.php和test.php在同一個域中)。
可能與'包括( 'file.php');' – Vytautas