2012-05-28 105 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在同一個域中)。