Home >Backend Development >PHP Tutorial >The wonderful use of the Output Buffer function_PHP tutorial

The wonderful use of the Output Buffer function_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 17:21:50762browse


In PHP programming, we often encounter some functions that directly generate output, such as passthru(), readfile(), var_dump(), etc. But sometimes we want to import the output of these functions into a file, or first Process and then output, or process the output of these functions as strings.
At this time we will use the Output Buffer (output buffer) function.
There are several main functions for processing output buffers:
ob_start() starts output buffering. At this time, PHP stops output, and all subsequent output is transferred to an internal buffer.
ob_get_contents() This function returns the contents of the internal buffer. This is equivalent to putting these The output becomes a string.
ob_get_ length() returns the length of the internal buffer.
ob_end_flush() ends the output buffer and outputs the contents of the buffer. After this, the output is normal.
ob_end_clean() ends the output buffer and throws away the contents of the buffer.
For example, the var_dump() function outputs the structure and content of a variable, which is useful during debugging.
But if the variable There are $#@60;, $#@62; and other HTML special characters in the content, which will be invisible when output to the web page. What should I do?
This problem can be easily solved by using the output buffer function.
ob_start();
var_dump($var);
$out = ob_get_contents();
ob_end_clean();
At this time, the output of var_dump() is already stored in $out. You can Output now:
echo "$#@60;pre$#@62;" . htmlspecialchars($out) . "$#@60;/pre$#@62;" ;
Or wait until the future, Or send this string to the template (Template) and then output it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532390.htmlTechArticleIn PHP programming, we often encounter some functions that directly generate output, such as passthru(), readfile( ), var_dump(), etc. But sometimes we want to import the output of these functions into a file, or...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn