Three tricks to make PHP engine run quickly_PHP Tutorial
PHP The unique syntax mixes C, Java, Perl and PHP's own innovative syntax. It can execute dynamic web pages faster than CGI or Perl. Compared with other programming languages, dynamic pages made with PHP embed programs into HTML documents for execution, and the execution efficiency is much higher than CGI that completely generates HTML tags. Here are three ways to make the PHP engine run quickly.
1. Code Optimization
When it comes to code optimization, maybe you think of neat and clear code, but the meaning of this article is not that, because if you want If you are looking for speed, you must make corresponding adjustments to the PHP source code. Generally speaking, redundant comments are removed to make the code unreadable. But for a programmer with good qualities, this is simply incredible. Fortunately, ZendTechnologies has released the Zend optimization engine to help you do this. It's free now, but you must follow the Zend Optimizer license. This product can optimize the intermediate code generated by the engine.
Installing this engine is relatively simple. After downloading the version corresponding to the platform, unzip the compressed file, then add the following two lines to the php.ini file, restart the web server, and you are done.
<ol class="dp-c"> <li class="alt"><span><span>zend_optimizer.optimization_level=15 </span></span></li> <li> <span>zend_extension=</span><span class="string">"/path/to/ZendOptimizer.so"</span><span> </span> </li> <li class="alt"><span>zend_loader.enable=Off </span></li> </ol>
If it is a Win32 platform, it should be:
<ol class="dp-c"> <li class="alt"><span><span>zend_optimizer.optimization_level=15 </span></span></li> <li> <span>zend_extension_ts=</span><span class="string">"C:\path\to\ZendOptimizer.dll"</span><span> </span> </li> <li class="alt"><span>zend_loader.enable=Off </span></li> </ol>
In fact, the third line is optional. Since it seems like turning off zend_loader will improve the speed a bit, it's worth putting this third line into php.ini . It should be noted that the prerequisite for turning it off is that you are not using the Zend encryption program.
2. Buffering
If we want to further improve the speed, we need to consider using buffering technology. There are some alternative solutions, including Zend Cache (beta version), APC, and Afterburner Cache, as well as jpCache, etc.
The above are buffer modules. They store the intermediate code generated by the first request for the .php file in the memory of the web server, and then return the "compiled" version for subsequent requests. Because this reduces disk reads and writes, and all work in memory, this process can significantly improve application performance. There are many such products readily available, so who should you choose?
- Zend Cache is a good commercial product. After loading those large PHP pages for the first time, you will obviously feel the speed increase, and the server will set aside more resources. Unfortunately, this product costs money, but in some cases, you don't want to skimp on the money.
- Afterburner Cache is a product of Bware Technologies. It is still in Beta version. It seems to be the same as Zend Cashe, but it cannot achieve as good results as Zend Cache, nor can it work with the Zend optimization engine, but it It's free, so I adopted this module.
- APC (Alternative PHP Cache) is another free module released by Community Connect that seems to be ready for production environments.
3. Web content compression
For increasingly congested networks, saving bandwidth is as valuable as saving water. According to IETF standards, most browsers should support content compressed using gzip. This means you can send gzip-compressed content to the browser, and the browser will transparently decompress the data.
mod_gzip is a free Apache module launched by Remote Communications, which can compress static Web content and send it to the browser. For most static web pages, this module is suitable. Although the folks at Remote Communications say that this module supports all those dynamic content generated by mod_php, mod_perl, mod, etc., it still doesn't seem to work. Judging from the mod_gzip mailing list, this problem is not expected to be solved until 1.3.14.6f.
If you want to compress dynamic content, we can use class.gzip_encode.php, a PHP class used at the beginning and end of the script. For the entire website, the functions in auto_prepend and auto_append of php.ini are called. For details, you can read the program of this class. This program is well commented and the author tells you almost everything. But before using it, your PHP must be compiled to support zlib.
For PHP 5, a new solution is to use ob_gzhandler, which can achieve the same effect as the above class. Simply add the following sentence to php.ini:
<ol class="dp-c"><li class="alt"><span><span>output_handler = ob_gzhandler ; </span></span></li></ol>
This enables PHP to activate output buffering and compress all output. If there is any special reason why you don’t want all the content to be compressed and output, you can add the following line to the .htaccess file to compress the files in the corresponding directory.
php_value output_handler ob_gzhandler
You can also add it directly in the PHP code:
<ol class="dp-c"><li class="alt"><span><span>ob_start(</span><span class="string">"ob_gzhandler"</span><span>); </span></span></li></ol>
This compression technology is very effective, but for Netscape Communicator users, because it cannot Compressed graphics files, so they don't appear to be sent completely, so compression of jpeg and gif files must be turned off. IE does not have this problem.
4. Conclusion
Using the techniques discussed in this article should improve your website performance, but it should be noted that:
PHP may not What is causing the bottleneck, double check other causes (eg: database)
You cannot adjust the server performance to the highest state. So before complaining about PHP and its buffering, consider whether it's time to upgrade your server or adopt dynamic load balancing technology.
Don’t underestimate content compression, and while you’re seeing speed improvements on your PHP applications on your 100 Mb intranet, don’t forget where your modem users are complaining about your 100Kb HTML pages.
I hope that the introduction of the above content can be helpful to you.

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

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,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

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.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
