PHP nested output buffer code instance and php nested code instance

Source: Internet
Author: User

PHP nested output buffer code instance and php nested code instance

PHP output cache can be nested. You can use ob_get_level () to output the nested level.
The test results are different in cli and browser (PHP5.4 ).

The manual description is as follows:

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

It is easy to output the correct output:
Copy codeThe Code is as follows:
Ob_end_clean ();
Echo ob_get_level (); // 0

Back to the topic:
Copy codeThe Code is as follows:
Ob_end_clean ();
 
Ob_start ();
Echo 'php1'; // It is not output on the page
$ A = ob_get_level ();
$ B = ob_get_contents (); // obtain the cache result and assign the variable
Ob_clean ();
 
Ob_start ();
Echo 'php2'; // This is not output on the page
$ C = ob_get_level ();
$ D = ob_get_contents (); // obtain the cache result and assign the variable
Ob_clean ();
 
Ob_start ();
Echo 'php3'; // This is not output on the page
$ E = ob_get_level ();
$ F = ob_get_contents (); // obtain the cache result and assign the variable
Ob_clean ();
 
Echo 'level: '. $ a.', ouput: '. $ B.' <br> ';
Echo 'level: '. $ c.', ouput: '. $ d.' <br> ';
Echo 'level: '. $ e.', ouput: '. $ f.' <br> ';

The result is as follows:
Copy codeThe Code is as follows:
Level: 1, ouput: php1
Level: 2, ouput: php2
Level: 3, ouput: php3

Of course, when you disable a buffer level, perform the following test:
Copy codeThe Code is 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 (); // clears the cache and closes 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 result is as follows:
Copy codeThe Code is 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.