Learning notes for PHP output control

Source: Internet
Author: User
Tags bool error handling php script phpinfo setcookie

1. Introduction

When the PHP script has output, the output control function can use these to control the output. This is useful in a variety of different situations, especially when the script starts outputting data and sends HTTP header information to the browser. The output control function does not affect the header information sent by the header () or Setcookie (), only affects the data between functions such as Echo and PHP blocks of code.

2, the role

1, as the official introduction, in order to avoid the output file header information such as header () and Setcookie () before the error occurred. You can use output-caching functions. The following code

The code is as follows
<?php
Ob_start (); To open the cache control function
echo ' Hello ';
Header (' location:http://www.111cn.net/');
?>

2), the output into the buffer, if you need to do other operations, such as the output to the cache file, the buffer content can be written to the cache file, and then output.

The code is as follows
Ob_start ();
echo ' Hello ';
$file = Ob_get_contents ();
Ob_end_clean ();
Write_cache ($file); Writes the buffer contents to the cache function, omitting the details

3, get some output of the built-in function without return value, such as Phpinnfo ();

The code is as follows
<?php
Ob_start ();
Phpinfo ();
$file = Ob_get_contents (); Phpinfo () Output content
Ob_end_clean ();

?>

4, when using the frame, we will find that the frame will be output buffer and then variable substitution. Final output.

In addition to these content, we can also use the output cache for error handling, the specific look at the code. We can get the error information and output it in a more friendly way.

The code is as follows
<?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 $a;

Attached: Ob_start () official explanation:

BOOL Ob_start ([Callback $output _callback [, int $chunk _size [, BOOL $erase]]]

This function opens the output buffer. When the output buffer is activated, the script will not output the content (except the HTTP header), and the content that needs to be output is stored in the internal buffer.

The contents of the internal buffer can be copied into 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 silently discards the contents of the buffer.

Output_callback

Optional parameter Output_callback function can be specified. This function treats a string as an argument and returns a string. The function will be invoked when the output buffer is (Ob_flush (), Ob_clean () or similar functions) flushed (sent out) or purged, or when the output buffer content is flushed to the browser at the end of the request. When Output_callback is invoked, it receives the contents of the output buffer as a parameter and expects to return a new output buffer as a result, and the newly returned output buffer content is sent to the browser. If this 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 bit field, which consists of Php_output_handler_start, Php_output_handler_cont, and Php_output_handler_end.

If Output_callback returns FALSE, its original input is sent directly 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 invoked from a callback function. If you call them from a callback function, the resulting behavior is ambiguous. If you want to delete the contents of the buffer, return a "" (empty string) from the callback function. You cannot use output buffering functions 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.