Home >Backend Development >PHP Tutorial >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?
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.