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 and 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 have prescribed for you to see if they are effective.
1. Code optimization
When it comes to code optimization, you may think of neat and clear code, but this is not the point 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:pathtoZendOptimizer.dll"
zend_loader.enable=Off
ah! That's not a mistake? Why three lines? Actually 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 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, and it seems that it can be used in 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
People from the Remotecommunications company said that this module supports all the dynamic content generated by mod_php, mod_perl, mod and so on, but it still doesn't seem to work. Judging from the mod_gzip mailing list, this problem will not 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 needs to 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 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:
ob_start("ob_gzhandler");
This compression technology is very effective, but for Netscape Communicator users, because graphics files cannot be compressed, they appear to be incompletely sent, so compression of jpeg and gif files must be turned off. IE does not have this problem.
Conclusion:
Using the techniques discussed in this article should improve your website performance, but there are a few caveats:
- PHP may not be the cause of the bottleneck, carefully check other causes (e.g. 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 (that's a lot of money).
- Don't underestimate content compression. While you're seeing speed improvements for your PHP applications on your 100 Mb intranet, don't forget where your modem users are complaining about your 100Kb HTML pages.