<?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":'';