PHP’s output cache can be nested. Use ob_get_level() to output the nesting level.
The test found that the output results were different under the cli and the browser (PHP5.4).
The manual instructions are 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 also very simple to output correctly:
Copy code The code is as follows:
ob_end_clean();
echo ob_get_level(); //0
Back to the topic:
Copy code The code is as follows:
ob_end_clean();
ob_start();
echo 'php1';//This will not be output on the page
$a = ob_get_level();
$b = ob_get_contents();//Get cached results and assign them to variables
ob_clean();
ob_start();
echo 'php2';//This will not be output on the page
$c = ob_get_level();
$d = ob_get_contents();//Get cached results and assign them to variables
ob_clean();
ob_start();
echo 'php3';//This will not be output on the page
$e = ob_get_level();
$f = ob_get_contents();//Get cached results and assign them to variables
ob_clean();
echo 'level:'.$a.',ouput:'.$b.'
';
echo 'level:'.$c.',ouput:'.$d.'
';
echo 'level:'.$e.',ouput:'.$f.'
';
The results are as follows:
Copy code 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:
Copy code 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(); //Clear 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:
Copy code The code is as follows:
level:1,ouput:php1
level:2,ouput:php2
level:2,ouput:php3