-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"
ob_get_cleanとrequire_ONCEのドキュメントをお読みになりましたか? –
本当に! require_once()をinclude()に変更するとすべてが解決されます。 – Casey
多くの出力バッファリングを使用することは、通常、あなたのコードを間違って構造化することを意味します – nogad