Three tricks to keep your PHP engine running at full speed

Source: Internet
Author: User
Tags apc html page ini php class php file php and php code zend
As a popular Web programming language, PHP's biggest advantage is speed. PHP4 has done very well in this area and you can hardly find a faster scripting language. But if your application load is very large, and the bandwidth is relatively small, or other bottlenecks affect your server performance, then you may wish to try the author for you to prescribe a few prescriptions to see if it is efficacious.





One, code optimization





when it comes to code optimization, maybe you think of neat and clear code, but this is not the meaning of this article, because if you want to find the speed, it is necessary to adjust the PHP source code accordingly. The general thing is to get rid of unnecessary annotations and make the code unreadable. But this is incredible for a good quality programmer. Fortunately Zend Technologies company released a Zend optimization engine that can help you do this. It's free now, but you have to follow Zend Optimizer license. This product can be optimized for the intermediate code generated by the engine.





install this engine is relatively simple, download the version of the corresponding platform after the release of compressed files, and then add the following two lines in the php.ini file, restart the Web server, it's done.





zend_optimizer.optimization_level=15





zend_extension= "/path/to/zendoptimizer.so"





Zend_loader.enable=off





if it's Win32 platform, it should be:





zend_optimizer.optimization_level=15





zend_extension_ts= "C:\\path\\to\\zendoptimizer.dll"





Zend_loader.enable=off





Ah! No mistake, right? What's three lines? In fact, the third line is optional. Because it looks like turning the zend_loader off can improve the speed, it's worth putting the third line in the php.ini. Note that the prerequisite for shutting down is that you are not using the Zend encryption program.





II, buffer





if you want to increase the speed further, we need to consider the use of buffer technology. There are some alternative solutions, including Zend cache (Beta version), APC, and Afterburner cache, plus jpcache.





These are all buffers, and they store the intermediate code generated by the first request for the. php file in the WEB server's memory, and then return a "compiled" version of the request. Because this reduces disk reads and writes, and all work in memory, this process can significantly improve application performance,





ready-made such products are more, in the end choose who?





Zend Cache is a good commercial product, the first time you load those large PHP page, you will obviously feel the speed of ascension, the server will set aside more resources. Unfortunately, this product is to spend money, but in some cases, you should not be stingy with these silver.





afterburner Cache is Bware technologies product, is still in Beta version, looks like Zend cashe, but it can not achieve Zend Cache as good effect, and can not and Zend excellent The engine worked together, but it was free, so I used this module.





APC (Alternative PHP Cache) is another free module released by Community Connect, which looks like it can be used in production environments.





Third, Web content compression





For an increasingly congested network, bandwidth savings are as much worth advocating as water conservation. According to IETF standards, most browsers should support content that uses gzip compression. That is, you can send the content with gzip compressed to the browser, the browser will be transparent to extract data.





Mod_gzip is a free Apache module launched by Remote Communications to compress static Web content and send it to browsers. This module is suitable for most static web pages. Although





Remotecommunications said the module supports all those mod_php, mod_perl,mod what generates dynamic content, but looks like it's still not working, from the Mod_gzip mailing list, This problem is estimated to be 1.3.14.6f to solve.





If you want to compress dynamic content, we can use class.gzip_encode.php, a PHP class that is used at the beginning and end of the script. For the entire site, the functions are called in the php.ini Auto_prepend and Auto_append. You can read the program in detail, the program is well commented, and the author has almost told you everything. However, before using, your PHP will be compiled to support zlib.





for PHP 4.0.4, a new solution is to use the Ob_gzhandler, can achieve the same effect as the above class, as long as the simple in php.ini add the following sentence can be:





Output_handler = Ob_gzhandler;





This allows PHP to activate the output buffer and compress all output. If there is any special reason not to let all the content compression output, you can use in the. htaccess file to add the following line, the corresponding directory to compress the files.





php_value Output_handler Ob_gzhandler





can also be added directly to the PHP code:





Ob_start ("Ob_gzhandler");





This compression technology is very effective, but for Netscape Communicator users, because the graphics file can not be compressed, so it does not appear to complete the send, so you must turn off the JPEG and GIF file compression, IE does not have this problem.





Conclusion:





using the techniques discussed in this article should improve your site performance, but note that:





-PHP may not be the cause of the bottleneck, double-check for other reasons (for example, database)





-You cannot adjust server performance to the highest level. So before blaming PHP and its buffers, consider whether to upgrade the server or adopt dynamic load balancing technology (which is a lot of money).





-Do not underestimate content compression, when you see the speed of PHP applications on your MB intranet, don't forget where users of your modem are complaining about your 100Kb HTML page.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.