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