有没有办法让这成为可能?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