This article mainly introduces the PHP nested output buffer Code instance, this article explains the nesting uses the OB series function instance, needs the friend to be possible to refer to under
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:
The code is as follows:
Ob_end_clean ();
Echo Ob_get_level (); 0
Back to the point:
The code is 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. '
';
echo ' Level: '. $c. ', ouput: '. $d. '
';
echo ' Level: '. $e. ', ouput: '. $f. '
';
The results are as follows:
The code is 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:
The 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 (); 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. '
';
echo ' Level: '. $c. ', ouput: '. $d. '
';
echo ' Level: '. $e. ', ouput: '. $f. '
';
The results are as follows:
The code is as follows:
Level:1,ouput:php1
Level:2,ouput:php2
Level:2,ouput:php3