我爱编程

PHP输出内容前关闭其他输出的使用

2018-04-16  本文已影响16人  云龙789

ob_end_clean

echo '不要输出';
$out = '可以输出';
ob_end_clean();
echo '这是我要的<br>';
echo $out;
image.png

如果没有 ob_end_clean(); 这行,echo '不要输出';这个也会输出。

如果想要一些固定的输出,在输出之前添加ob_get_contents();函数即可,但要在自己的输出之前使用ob_end_clean();函数关闭以前的输出

echo '不要输出';
ob_end_clean();
echo '可以输出';
$out = ob_get_contents();
echo $out;

如果想要输出某些值,可以赋值一个变量

image.png

官网完整的demo是这样的,我觉得完整写法可能是需要顶部ob_start(); 尾部ob_end_clean();

<?php
ob_start();
echo "Hello ";
$out1 = ob_get_contents();
echo "World";
$out2 = ob_get_contents();
ob_end_clean();
var_dump($out1, $out2);
?>

这种方式也可以直接作为ajax的请求输出

上一篇 下一篇

猜你喜欢

热点阅读