2012-05-28 102 views

回答

1

我用:

$foo = 'bar'; 
require_once('./file.php'); 

而且在file.php它只是:

echo $foo; 
0
ob_start(); 
include 'file.php'; 
echo ob_get_clean(); 

但实际上,这似乎是一个相当无意义的事情。如果您只想file.php评估并且输出它的内容,只需include该文件。

+0

是,使用OB-功能似乎在这种情况下是无意义的。这应该避免,因为它不需要提供预期的输出。 –

1

我会做这样的:

// 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在同一个域中)。