1. Introduction
When PHP scripts have output, the output control function can use these to control the output. This is useful in many different situations, especially when the script starts to output data, it sends the http header information to the browser. The output control function does not affect the header information sent by the header () or setcookie (). It only affects data between functions such as echo and PHP code blocks.
2. Functions
1) According to the official introduction, in order to avoid errors that occur before output file header information such as header () and setcookie. You can use the output cache function. The following code
The code is as follows: |
Copy code |
<? Php Ob_start (); // open the cache control function Echo 'hello '; Header ('Location: http://www.111cn.net /'); ?>
|
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 it.
The code is as follows: |
Copy code |
Ob_start (); Echo 'hello '; $ File = ob_get_contents (); Ob_end_clean (); Write_cache ($ file); // write the buffer content to the cache function. 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: |
Copy code |
<? Php Ob_start (); Phpinfo (); $ File = ob_get_contents (); // phpinfo () Output Content Ob_end_clean (); ?>
|
4) when using the framework, we will find that the framework will buffer the output and replace the variables. Final output.
In addition to this content, we can also use the output cache for error handling, depending on the code. We can get the error information and output it in a more friendly way.
The code is as follows: |
Copy code |
<? Php Function display_error () { If ($ errors = error_get_last ()) { Return var_export ($ errors, TRUE ); } Return FALSE; } $ Dis_error = 'display _ error '; // $ A = 3; Ob_start ($ dis_error ); Echo $;
|
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 does not output content (except the http header). On the contrary, the content to be output is 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, using the ob_end_clean () function will silently discard 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 washed (sent) or cleaned by (ob_flush (), ob_clean (), or similar functions; or the function will be called when the output buffer content is washed to the browser at the end of the request. When output_callback is called, it will receive the content of the output buffer as a parameter and expect to return a new output buffer as the result. The content of the new output buffer will be sent to the browser. If output_callback is not a function that can be called, FALSE is returned.
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 unclear. 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.