2017-04-11 94 views
-1

我有一個名爲test.php的PHP文件,其中包含以下代碼。爲什麼多次調用緩衝區時不輸出任何內容?

<?php 
$text = 'Grrrrr'; 
ob_start(); 
require_once('testinc.php'); 
$html2 = ob_get_clean(); 
echo '<pre>'; 
var_dump($html2); 
echo '</pre>'; 


$text = 'wtf'; 
ob_start(); 
require_once('testinc.php'); 
$html2 = ob_get_clean(); 
echo '<pre>'; 
var_dump($html2); 
echo '</pre>'; 


testing('blah'); 
testing('123'); 

function testing($text) { 
    ob_start(); 
    require_once('testinc.php'); 
    $html = ob_get_clean(); 
    echo '<pre>'; 
    var_dump($html); 
    echo '</pre>'; 
} 

testinc.php文件包含以下代碼。

<?php 
echo $text; 
?> 
testing 

當我運行代碼時,輸​​出如下所示。

string(13) "Grrrrrtesting" 
string(0) "" 
string(0) "" 
string(0) "" 

爲什麼不大於第一次工作緩衝區等,我怎樣才能輸出到這個樣子?

string(13) "Grrrrrtesting" 
string(10) "wtftesting" 
string(11) "blahtesting" 
string(10) "123testing" 
+1

您是否閱讀過ob_get_clean和require_ONCE的文檔? –

+0

真的嗎?!將require_once()更改爲include()可以解決所有問題。使用大量輸出緩衝的 – Casey

+0

通常意味着您的代碼結構錯誤 – nogad

回答

0

解決方案是不使用require_once()。將代碼更改爲包含(),並且它一切正常。