<?php
function ob($function){
ob_start();
$function();
$produced = ob_get_contents();
ob_end_clean();
return $produced;
}
echo ob(function(){echo 123;})=='123'?"echo captured\n":'';
示例 #1 输出控制示例
<?php
ob_start();
echo "Hello\n";
setcookie("cookiename", "cookiedata");
ob_end_flush();
?>
在上面的示例中,来自 echo 的输出将被存储在输出缓冲区中,直到调用 ob_end_flush()。在此期间,对 setcookie() 的调用成功存储了 Cookie,而不会引起错误。(在已经发送数据后,通常不能向浏览器发送标头。)
<?php
function ob($function){
ob_start();
$function();
$produced = ob_get_contents();
ob_end_clean();
return $produced;
}
echo ob(function(){echo 123;})=='123'?"echo captured\n":'';