在请求一个PHP的过程当中,实际上通过三个缓存:php
程序缓存html
ob缓存apache
浏览器缓存.浏览器
1.在php.ini 配置 ;output_buffering = 4096 这里去掉;号便可 2 在php页面中使用 ob_start();
经过php.ini 打开的,则做用于全部的php页面 。使用ob_start()打开则只做用于该页面缓存
在服务中,若是咱们开启了ob缓存,则echo数据首先放入到ob中函数
当PHP页面执行到最后,则会把ob缓存的数据(若是有的话), 强制刷新到程序缓存,而后经过apache对数据封装成http响应包,返 回给浏览器spa
若是没有ob,全部的数据直接放入程序缓存。 header信息无论你是否开启ob,老是放入到程序缓存。code
//在当前页面中开启ob,注意callback ob_start($callback);
//获取当前ob缓存中的内容 ob_get_contents()
//获取当前ob缓存中的内容,而且清空当前的ob缓存 ob_get_clean()
//将ob缓存中的内容,刷到程序缓存中,但并无关闭ob缓存 ob_flush()
//关闭ob缓存,并将数据刷回到程序缓存中 ob_end_flush()
//将ob缓存中的内容清空 ob_clean()
//将ob缓存中的数据清空,而且关闭ob缓存 ob_end_clean()
<?php ob_start("callback_func"); function callback_func($str){ return "callback".$str; } echo "123";//输出:callback123
<?php echo "before_header"; header("Content-type:text/html;charset=utf-8"); echo "after_header";
输出:orm
Warning: Cannot modify header information - headers already sent by (output started at /Users/shuchao/Desktop/test.php:2) in /Users/shuchao/Desktop/test.php on line 3
在发送header前开启ob,则全部的echo内容都会到ob里面,从而解决错误。htm
<?php ob_start(); echo "before_header\n"; header("Content-type:text/html;charset=utf-8"); echo "after_header\n";
输出
before_header after_header
更多精彩,请关注公众号“聊聊代码”,让咱们一块儿聊聊“左手代码右手诗”的事儿。