Wonderful use of Output & nbsp; Buffer & nbsp; (Output Buffer) functions

Source: Internet
Author: User
In PHP programming, we often encounter some functions that directly generate output, such as passthru (), readfile (), and var_dump. but sometimes we want to export the output of these functions to a file, or process and output them first, or use the output of these functions as a PHP programming, we often encounter some functions that directly generate output, such as passthru (), readfile (), and var_dump. however, sometimes we want to export the output of these functions to a file, or process the output of these functions as strings.
In this case, we need to use the Output Buffer function. There are several important functions for processing the Output Buffer:
Ob_start () begins to buffer the output, and PHP ends the output, after which the output is forwarded to an internal buffer. the ob_get_contents () function returns the content of the internal buffer. this means that all these outputs are converted into strings. ob_get _ length () returns the length of the internal buffer. ob_end_flush () ends the output buffer and outputs the content in the buffer. after that, the output is normal. ob_end_clean () ends the output buffer and discards the content in the buffer. for example, the var_dump () function outputs the structure and content of a variable, which is useful during debugging.
However, if the variable contains special characters such as <,> in HTML, the output will be invisible to the webpage. what should I do?
The output buffer function can easily solve this title. ob_start ();
Var_dump ($ var );
$ Out = ob_get_contents ();
Ob_end_clean ();
At this time, the output of var_dump () already exists in $ out. now you can output: echo'
'. Htmlspecialchars ($ out ).'
'; Or wait until the future, or send this string to the Template before outputting it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.