Home >Backend Development >PHP Tutorial >PHP nested output buffer code example, php nested code example_PHP tutorial

PHP nested output buffer code example, php nested code example_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-13 09:54:13923browse

PHP nested output buffer code example, PHP nested code example

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/998551.htmlTechArticlePHP nested output buffer code example, php nested code example PHP's output cache can be nested. Use ob_get_level() to output the nesting level. The test found that the output under cli and browser...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn