ob_clean() - php 输出缓冲函数
ob_clean()
(PHP 4 >= 4.2.0, PHP 5, PHP 7)
清空(擦掉)输出缓冲区
说明
ob_clean(void): void此函数用来丢弃输出缓冲区中的内容。
此函数不会像ob_end_clean()函数那样销毁输出缓冲区。
输出缓冲必须已被ob_start()以PHP_OUTPUT_HANDLER_CLEANABLE标记启动。否则ob_clean()不会有效果。
返回值
没有返回值。
参见
ob_flush()
冲刷出(送出)输出缓冲区中的内容ob_end_flush()
冲刷出(送出)输出缓冲区内容并关闭缓冲ob_end_clean()
清空(擦除)缓冲区并关闭输出缓冲
@cornel: It's easy enough to say "Don't do that" when you think you've got the person right in front of you. But one doesn't always have the original coder, or even one of a dozen of the original coders. Are you really suggesting that it would be wrong to use this function as a band-aid when the alternative may be looking through hundreds of source files you didn't write for errors you didn't introduce? To your point, though, it is (or should be) a commonly accepted best practice to not put closing PHP tags at the end of files. When, however, enforcing that would take a time machine, it's appropriate to use ob_clean() as a band-aid to make dynamically generated images work as expected.
Don't use ob_clean() to clear white-space or other unwanted content "accidentally" generated by included files. Included files should not generate unwanted content in the first place. If they do, You are doing something wrong, like inserting white-space after "?>" (there should not be a "?>" at the end of PHP files: http://php.net/manual/en/language.basic-syntax.phptags.php ).
I find this function incredibly useful when manipulating or creating images in php (with GD). I spent quite a while searching through a large number of included files to find where I had a undesired space after php's ending tag - as this was causing all my images on the fly to break due to output already being set. Even more annoying was that this was not caught not php's error reporting so there was no reference to the problem line(s) in my log file. I don't know why error reporting wouldn't catch this since it was set to accept warnings, and the same thing had been caught in the past. Nevertheless, I never did find the line(s) that were adding extra spaces or new lines before my images were being generated, but what I did instead was add this handy function right before my image manipulation code and right after the include/require code. For example: While this may seem trivial a trivial use of the function, it in fact is incredibly useful for insuring no extra spaces or new lines have already been output while making images in php. As many of you probably already know, extra lines, spacing and padding that appears prior to image-code will prevent the image from being created. If the file "lib/somelibrary.php" had so much as an extra new line after the closing php tag then it would completely prevent the image from working in the above script. If you work on an extremely large project with a lot of source and required files, like myself, you will be well-advised to always clear the output buffer prior to creating an image in php.
In case there is nothing output yet to the buffer and then implementing the ob_clean() a (soft) error notice would be generated such as the following: Notice: ob_clean() [ref.outcontrol]: failed to delete buffer. No buffer to delete. in [file-path] you could do a simple check with "ob_get_contents()" to make sure that there is anything in the buffer.
Note that ob_clean() and ob_end_clean() do not clean header sent with header()
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)