<?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":'';
Example #1 输出控制举例
<?php
ob_start();
echo "Hello\n";
setcookie("cookiename", "cookiedata");
ob_end_flush();
?>
在上面的例子中,echo函数的输出将一直被保存在输出缓冲区中直到调用 ob_end_flush() 。同时,对setcookie()的调用也成功存储了一个cookie,而不会引起错误。(正常情况下,在数据被发送到浏览器后,就不能再发送http头信息了。)
<?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":'';