PHP output cache usage example tutorial
This article introduces the usage of PHP output cache. The so-called output cache in PHP means that the echo or other output commands in the code are first written to the PHP buffer when executed. After the script is executed or the output cache command is forced to be executed, it is The data will be output to the browser.
1. What is php output cache? The so-called output cache in PHP means that the echo or other output commands in the code are first written to the PHP buffer when executed. The data will be output to the browser only after the script is executed or the output cache command is forced to be executed ( Among them, php buffer is the output_buffering set in php.ini. The default is on, indicating unlimited size, which can be changed to a number to limit the size). Example: echo 'hlmblog.com'; echo 'technology'; echo 'share';These two echoes are inserted into the buffer in order. The data will be output to the browser only after the script execution is completed or cache output is forced. Output echo data in real time: ob_end_flush(); //Turn off the php cache, or execute ob_flush() before flush(), as explained below echo str_pad(" ", 256); for ($i=5; $i>0; $i--) { echo $i. ''; flush(); sleep(1); } Note: 1: The difference between flush and ob_flush: At first glance, the two are very similar, and the explanations in many manuals are unclear and ambiguous. In fact, there is a big difference between the two. When php.ini does not enable the php buffer cache, the content output by the php script will be waiting for output on the server and will not be saved to the output cache because the cache is not turned on. At this time, flush can be used to immediately flush the content waiting for output. The output is sent to the client (browser or other output terminal). When php.ini turns on the php buffer cache, the first step of the php script output content is to store it in the output cache. At this time, there is no data in the output content. Using flush has no effect and the data cannot be obtained. . Therefore, we must first use ob_flush to take out the content in the output cache and put it in a state waiting for output, and then use flush to send the content to the client. The order of execution is ob_flush first and then flush. Therefore, to achieve real-time output, either use ob_end_flush to first turn off the php output cache and then flush directly, or first ob_flush and then flush. 2: The browser cannot output real-time data The modified code is output once in Chrome, Firefox, IE and other browsers: ob_end_flush(); //Turn off the php cache, or ob_flush(); before flushing echo str_pad(" ", 256); for ($i=5; $i>0; $i--) { echo $i; flush(); sleep(1); }Just add an html tag and you can output it in real time. The reason is: it will be output immediately only when it encounters an html tag. It is really magical. Fortunately, the output content usually contains html tags and rarely contains plain text. Solution: Add a carriage return or other html tag to solve the problem. 2. Example of controlling cache output 1. Generate static pages Static pages load quickly echo str_pad('', 1024);//make the buffer overflow ob_start();//Open the buffer $content = ob_get_contents();//Get the content output by the page $f = fopen('./index.html', 'w'); fwrite($f, $content);//Write content into txt file fclose($f); ob_end_clean();//Clear and close the buffer2. Capture output function test($param) { if($param) { ob_start(); eval($param); $contents = ob_get_contents(); ob_end_clean(); }else { echo 'Unfortunately no output'; exit(); } return $contents; } |

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
