


Detailed explanation of PHP output cache ob series functions_PHP tutorial
Basic principle of ob: If the ob cache is turned on, the echo data is first placed in the ob cache. If it is header information, it is placed directly in the program cache. When the page is executed to the end, the ob cached data will be placed in the program cache, and then returned to the browser in turn.
Let me talk about the basic functions of ob:
1) Prevent the use of functions such as setcookie(), header() or session_start() to send header files after the browser has output. error. In fact, it is better to use this kind of usage less often and develop good coding habits.
2) Capture the output of some unobtainable functions. For example, phpinfo() will output a lot of HTML, but we cannot use a variable such as $info=phpinfo(); to capture it. At this time, ob will be useful. .
3) Process the output content, such as gzip compression, conversion between Simplified and Traditional Chinese, and some string replacement.
4) Generating static files is actually capturing the output of the entire page and then saving it as a file. Often used in HTML generation or full page caching.
Regarding the GZIP compression mentioned in the third point just mentioned, many people may want to use it, but have not actually used it. In fact, by slightly modifying the code, you can achieve gzip compression of the page.
Content to be cached
Yes, Just add a callback function called ob_gzhandler, but there are some minor problems with this. First, it requires zlib support, and second, it does not determine whether the browser supports gzip (it seems to support it now, and all iPhone browsers seem to support it).
The previous approach was to determine whether the browser supports gzip, then use the third-party gzip function to compress the content of ob_get_contents(), and finally echo.
1. Collection of commonly used functions in ob series functions
ob_start(); //Open an output buffer. All output information is no longer sent directly to the browser, but is saved in the output buffer.
ob_clean(); //Delete the contents of the internal buffer without closing the buffer (no output).
ob_end_clean(); //Delete the contents of the internal buffer and close the buffer (no output).
ob_get_clean(); //Return the contents of the internal buffer and close the buffer. Equivalent to executing ob_get_contents() and ob_end_clean()
ob_flush(); sends the content of the buffer and deletes the content of the buffer without closing the buffer.
ob_end_flush(); //Send the contents of the internal buffer to the browser, delete the contents of the buffer, and close the buffer.
ob_get_flush(); //Return the contents of the internal buffer, close the buffer, and then release the contents of the buffer. Equivalent to ob_end_flush() and returns the buffer contents.
flush(); // Output the content released by ob_flush and the content not in the PHP buffer to the browser; refresh the content of the internal buffer and output it.
ob_get_contents(); //Return the contents of the buffer without output.
ob_get_length(); //Returns the length of the internal buffer. If the buffer is not activated, this function returns FALSE.
ob_get_level(); //Return the nesting level of the output buffering mechanism.
ob_get_status(); //Get status of output buffers.
ob_implicit_flush(); //Turn on or off absolute refresh. The default is off. After turning on ob_implicit_flush(true), the so-called absolute refresh means that when an output statement (e.g: echo) is executed, the output is sent directly to The browser no longer needs to call flush() or wait until the end of the script to output.
ob_gzhandler //ob_start callback function, uses gzip to compress the contents of the buffer.
ob_list_handlers //List all output handlers in use
output_add_rewrite_var //Add URL rewriter values
output_reset_rewrite_vars //Reset URL rewriter values
The behavior of these functions is affected by the php_ini settings:
output_buffering //When this value is ON, output control will be used in all scripts; if the value is a number, it represents the maximum byte limit of the buffer , when the cached content reaches the upper limit, the content in the current buffer will be automatically output to the browser.
output_handler //This option can redirect all output of the script to a function. For example, when output_handler is set to mb_output_handler(), the character's encoding will be modified to the specified encoding. Any processing functions set will automatically handle output buffering.
implicit_flush //The function is the same as ob_implicit_flush, the default is Off.
2. Examples
1. There can be echo code before the header() function
The Output Control function allows you to freely control the output of data in the script. It is very useful, especially when you want to output the file header after the data has been output.
The output control function does not affect the file header information sent using header() or setcookie(), only those data blocks similar to echo() and PHP code.
echo "Hellon"; //Output
header(“location:index.php”); //Redirect the browser to index.php
ob_end_flush(); //Output all content to the browser
Everyone who knows the header() function knows that this function will send a file header to the browser, but if there is any output before using this function (including empty output, such as spaces, carriage returns and line break) will prompt an error. If we remove ob_start() in the first line and then execute this program, we will find that we get an error message: "Header had all ready send by"! But with ob_start, there will be no error message. The reason is that when the buffer is opened, the characters after echo will not be output to the browser, but will be retained on the server. They will not be output until you use flush or ob_end_flush, so it will not Any file header output errors!
2. Save the output of the phpinfo() function
fwrite($file, $info); //Write information to info.txt
fclose($file); //Close the file info.txt
3. Static template technology
The so-called static template technology is to use a certain method to enable users to get the html page generated by PHP on the client side. If this HTML page will no longer be updated, then when another user browses this page again, the program will no longer call PHP and related databases. For some websites with a large amount of information, such as sina, 163, and sohu. The benefits of technology like this are huge.
Copy code
3. Output cache handle ob_gzhandler
PHP4.0.4 has a new output cache handler ob_gzhandler, which is similar to the previous class, but its usage is different. The content to be added to php.ini when using ob_gzhandler is as follows:
Copy code
The code is as follows:
Copy code
The code is as follows:
Copy code
The code is as follows:
The method of using the output cache handle is indeed very effective and does not bring any special load to the server. But it must be noted that Netscape Communicator has poor support for compressed graphics, so unless you can ensure that all users use IE browser, you should disable compressed JPEG and GIF graphics. In general, this compression works for all other files, but it is recommended that you test it separately for each browser, especially if you use special plug-ins or data viewers. This is especially important.
Notes:
1. The output_buffering of some web servers defaults to 4069 characters or larger, that is, the output content must reach 4069 characters before the server flushes the output buffer. In order to ensure that the flush is effective, It is best to have the following statement before the ob_flush() function:
2. The ob_* series of functions operate the output buffer of PHP itself, so ob_flush only refreshes PHP's own buffer, while flush refreshes the apache buffer. Therefore, the correct order to use the two is: ob_flush first, then flush. ob_flush releases data from PHP's buffer, and flush sends all data in/out of the buffer to the browser.
3. Don’t mistakenly think that after using ob_start(), the script’s echo/print and other output will never be displayed in the browser. Because after the PHP script ends, the buffer will be automatically refreshed and the content will be output.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),