Php output control learning Notes

Source: Internet
Author: User
Php output control only affects echo output, but does not affect functions such as in-place cookies, sessions, and headers. Next, let's take a look at this article. 1. introduction when PHP scripts have output, these output control functions can be used to control the output, which is different in many ways... php output control only affects echo output, but does not affect functions such as in-place cookies, sessions, and headers. Next, let's take a look at this article.

1. Introduction

When PHP scripts have outputs, the output control function can use these to control the output, which is useful in many different situations, especially when the script starts to output data, send the http header information to the browser. the output control function does not affect the file header information sent by the header () or setcookie (). it only affects data between functions such as echo and PHP code blocks.

2. Functions

1) as the official introduction said, in order to avoid errors that occur before output file header information such as header () and setcookie (), you can use the output cache function with the following code:

 

2) put the output content into the cache. if you need other operations, such as writing the output to the cache file, you can write the buffer content to the cache file and then output the code as follows:

Ob_start (); echo 'hello'; $ file = ob_get_contents (); ob_end_clean (); write_cache ($ file); // write the buffer content to the cache function, so the implementation details are omitted.

3) obtain the output content of some built-in functions without return values, such as phpinnfo (). The code is as follows:

  

4) when using the framework, we will find that the framework will buffer the output, replace the variables, and finally output.

In addition to this content, we can also use the output cache for error processing. For more information about the code, we can get the error information and output it in a more friendly way. The code is as follows:

   

Appendix: ob_start () official explanation:

Bool ob_start ([callback $ output_callback [, int $ chunk_size [, bool $ erase])

This function enables the output buffer. when the output buffer is activated, the script will not output the content (except the http header). On the contrary, the content to be output will be stored in the internal buffer.

The content of the internal buffer can be copied to a string variable using the ob_get_contents () function. to output the content stored in the internal buffer, you can use the ob_end_flush () function. In addition, you can use the ob_end_clean () function () the function silently discards the buffer content.

Output_callback

The optional parameter output_callback can be specified. this function treats a string as a parameter and returns a string. when the output buffer is (ob_flush (), ob_clean (), or similar function) this function is called when the output buffer content is washed to the browser at the end of the request. when output_callback is called, it takes the content of the output buffer as a parameter and expects to return a new output buffer as the result. the content of the returned output buffer will be sent to the browser, if output_callback is not a function that can be called, this function returns FALSE.

If the callback function has two parameters, the second parameter is supplemented by a single-bit field, which is composed of PHP_OUTPUT_HANDLER_START, PHP_OUTPUT_HANDLER_CONT, and PHP_OUTPUT_HANDLER_END.

If output_callback returns FALSE, the original input is directly sent to the browser.

This parameter output_callback can be avoided by directly giving a NULL value.

Ob_end_clean (), ob_end_flush (), ob_clean (), ob_flush () and ob_start () cannot be called from a callback function. if they are called from the callback function, the generated behavior is ambiguous. if you want to delete the buffer content, return a "(null string)" from the callback function ), you cannot use an output buffer function like print_r ($ expression, true) or highlight_file ($ filename, true) from a callback function.

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.