Home > Backend Development > PHP Tutorial > PHP caching mechanism

PHP caching mechanism

亚连
Release: 2023-03-25 18:20:01
Original
4153 people have browsed it

Here, we use a code example to further understand PHP's own caching mechanism. In future studies, we will also have a general understanding of the concept of caching.


Use php's own caching mechanism

If you want to test php's own caching mechanism, you need to configure the php.ini file

display_errors=On
output_buffering=Off
error_reporting= 设置错误级别
Copy after login

Two buffers: outputbuffer and program cache
After the ob cache is closed, it is placed in the program cache. The program cache must be placed behind the header, otherwise an error will be reported

Function:

ob_start(); //开启缓存
ob_clean(); //清空 outputbuffer的内容
ob_end_clean(); //关闭ob缓存,同时清空
ob_flush(); //输出ob内容,并清空,但不关闭
ob_end_flush(); //把ob缓存的内容输出,并关闭ob
ob_get_contents();  //获取output_buffering的内容
Copy after login

Test Question:

<?php 
ob_start();
echo "abc";
header("content-type:text/html;charset=utf-8");
echo "hello";
ob_clean();
echo "aa";
header("content-type:text/html;charset=utf-8");
?>
Copy after login

Output: aa
No error
Not closed, aa is put into ob

<?php 
ob_start();
echo "abc";
header("content-type:text/html;charset=utf-8");
echo "hello";
ob_end_clean();
echo "aa";
header("content-type:text/html;charset=utf-8");
?>
Copy after login

Output: aa
Error
aa is put into the program Cache

<?php 
ob_start();
echo "abc";
header("content-type:text/html;charset=utf-8");
echo "hello";
ob_flush();
echo "aa";
echo ob_get_contents();
?>
Copy after login

Output: abchelloaaaa
Wait for the next output together

<?php 
ob_start();
echo "abc";
header("content-type:text/html;charset=utf-8");
echo "hello";
ob_end_flush();
echo "aa";
echo ob_get_contents();
?>
Copy after login

Output: abchelloaaabchelloaa
Finally close it

The above is the cache of PHP itself that I organized Mechanism, I hope future study will be helpful to everyone.

Related articles:

Must understand the php caching mechanism

Simple php caching class sharing php Cache mechanism_php example

PHP page static study notes three: Use PHP caching mechanism to complete staticization

The above is the detailed content of PHP caching mechanism. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template