Output Buffer(输出缓冲)函数的妙用
在PHP编程中, 我们经常会遇到一些直接产生输出的函数, 如passthru(),readfile(), var_dump() 等. 但有时我们想把这些函数的输出导入到文件中,或者先经过处理再输出, 或者把这些函数的输出作为字符串来处理.
这时我们就要用到 Output Buffer(输出缓冲) 函数了.
处理输出缓冲的函数主要有这么几个:
ob_start() 开始输出缓冲, 这时PHP停止输出, 在这以后的输出都被转到一个内部的缓冲里.
ob_get_contents() 这个函数返回内部缓冲的内容. 这就等于把这些输出都变成了字符串.
ob_get_ length() 返回内部缓冲的长度.
ob_end_flush() 结束输出缓冲, 并输出缓冲里的内容. 在这以后的输出都是正常输出.
ob_end_clean() 结束输出缓冲, 并扔掉缓冲里的内容.
举个例子, var_dump()函数输出一个变量的结构和内容, 这在调试的时候很有用.
但如果变量的内容里有 $#@60; , $#@62; 等HTML的特殊字符, 输出到网页里就看不见了. 怎么办呢?
用输出缓冲函数能很容易的解决这个问题.
ob_start();
var_dump($var);
$out = ob_get_contents();
ob_end_clean();
这时var_dump()的输出已经存在 $out 里了. 你可以现在就输出:
echo "$#@60;pre$#@62;" . htmlspecialchars($out) . "$#@60;/pre$#@62;" ;
或者等到将来, 再或者把这个字符串送到模板(Template)里再输出.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment