This article presents practical solutions for PHP optimization, buffering, and compression.
As a popular Web programming language, PHP's biggest advantage is speed. PHP4 has done this very well, and you can hardly find a faster scripting language than it. But if your application load is heavy, your bandwidth is relatively small, or there are other bottlenecks that affect your server performance, then you might as well try some of the prescriptions I've prescribed for you to see if they work.
1. Code Optimization
When it comes to code optimization, you may think of neat and clear code, but this is not the meaning of this article, because if you want to seek 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, Zend Technologies 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.
zend_optimizer.optimization_level=15
zend_extension="/path/to/ZendOptimizer.so"
zend_loader.enable=Off
If it is a Win32 platform, it should be:
zend_optimizer.optimization_level=15
zend_extension_ts="C:path o endOptimizer.dll"
zend_loader.enable=Off
! That's not a mistake? Why three lines? Actually the third line is optional. Since it seems like turning off zend_loader can improve speed a bit, it's worth putting this third line in 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.
The above are all 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 and is currently 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 is free, so I This module is used.
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 crowded 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
people from the Remotecommunications company said that this module supports all the dynamic content generated by mod_php, mod_perl, mod and so on, 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 we 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 4.0.4, 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:
output_handler = ob_gzhandler;
This allows PHP activates output buffering and compresses 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
can also be added directly in the PHP code:
ob_start("ob_gzhandler");
This compression technology is very effective, but for Netscape Communicator users, it looks incomplete because it cannot compress graphics files? Mu⑺Stop? What is the problem with the compression of jpeg and gif files? IE does not have this problem.
Conclusion:
Using the techniques discussed in this article should improve your website performance, but be aware that:
- PHP may not be the cause of the bottleneck, double check for other causes (e.g. database)
- You cannot maximize server performance state. So before complaining about PHP and its buffering, consider whether it's time to upgrade your server or adopt dynamic load balancing technology (that's a lot of money).
- Don't underestimate content compression. 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.