Home > Article > Backend Development > Sharing of specific usage skills of PHP function ob_star()_PHP tutorial
Today we will introduce to you about 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 process it first and then output it, or process the output of these functions as a string.
In this case we need to use the PHP function ob_star( ).
There are mainly several functions that handle output buffering:
PHP function ob_star() starts output buffering. At this time, PHP stops outputting, and all subsequent outputs are redirected to In an internal buffer.
ob_get_contents() This function returns the contents of the internal buffer. This is equivalent to turning these outputs into strings.
ob_get_length() Returns the contents of the internal buffer Length.
ob_end_flush() ends the output buffer and outputs the contents of the buffer. After this, the output is normal output.
ob_end_clean() ends the output buffer and discards the contents of the buffer
For example, the var_dump() function outputs the structure and content of a variable, which is very useful during debugging.
But if the content of the variable contains HTML such as < , > The special characters are invisible when output to the web page. What should I do?
Using the PHP function ob_star() can easily solve this problem.
<ol class="dp-xml"> <li class="alt"><span><span>ob_start(); </span></span></li> <li><span>var_dump($var); </span></li> <li class="alt"> <span>$</span><span class="attribute">out</span><span> = </span><span class="attribute-value">ob_get_contents</span><span>(); </span> </li> <li><span>ob_end_clean(); </span></li> </ol>
This The output of var_dump() is already stored in $out. You can output now:
<ol class="dp-xml"><li class="alt"><span><span>echo '</span><span class="tag"><</span><span> </span><span class="tag-name">pre</span><span class="tag">></span><span>' . htmlspec<br>ialchars($out) . '</span><span class="tag"><</span><span> /pre</span><span class="tag">></span><span>' ; </span></span></li></ol>
or wait until the future, or send this string to the template (Template) and then output.
The above is a comparison of the functional advantages of the PHP function ob_star().