PHP Nested output Buffer Code instance _php instance

Source: Internet
Author: User
Tags garbage collection

The output cache of PHP can be nested. You can output nesting levels with Ob_get_level ().
The test found that the output was different between the CLI and the browser (PHP5.4).

The manual describes the following:

Ob_get_level () would always return 0 inside a destructor.
This happens because the garbage collection for output buffers has already do before the destructor is called

It's also easy to get the right output:

Copy Code code as follows:

Ob_end_clean ();
Echo Ob_get_level (); 0

Back to the point:

Copy Code code as follows:

Ob_end_clean ();

Ob_start ();
Echo ' PHP1 ';//This does not output in the page
$a = Ob_get_level ();
$b = ob_get_contents ()//Get cache result, assign variable
Ob_clean ();

Ob_start ();
Echo ' PHP2 ';//This does not output in the page
$c = Ob_get_level ();
$d = ob_get_contents ()//Get cache result, assign variable
Ob_clean ();

Ob_start ();
Echo ' php3 ';//This does not output in the page
$e = Ob_get_level ();
$f = ob_get_contents ()//Get cache result, assign variable
Ob_clean ();

echo ' Level: '. $a. ', ouput: '. $b. ' <br> ';
echo ' Level: '. $c. ', ouput: '. $d. ' <br> ';
echo ' Level: '. $e. ', ouput: '. $f. ' <br> ';

The results are as follows:

Copy Code code as follows:

Level:1,ouput:php1
Level:2,ouput:php2
Level:3,ouput:php3

Of course, when you turn off a certain level of buffering, test as follows:

Copy Code code as follows:

Ob_end_clean ();

Ob_start ();
Echo ' PHP1 ';
$a = Ob_get_level ();
$b = Ob_get_contents ();
Ob_clean ();

Ob_start ();
Echo ' PhP2 ';
$c = Ob_get_level ();
$d = Ob_get_contents ();
Ob_end_clean (); Empty the cache and close the cache

Ob_start ();
Echo ' php3 ';
$e = Ob_get_level ();
$f = Ob_get_contents ();
Ob_clean ();

echo ' Level: '. $a. ', ouput: '. $b. ' <br> ';
echo ' Level: '. $c. ', ouput: '. $d. ' <br> ';
echo ' Level: '. $e. ', ouput: '. $f. ' <br> ';

The results are as follows:

Copy Code code as follows:

Level:1,ouput:php1
Level:2,ouput:php2
Level:2,ouput:php3

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.