How to improve PHP running speed? PHP7 PHP website php empty

Source: Internet
Author: User
Tags apc php source code php website
One of the biggest advantages of using PHP is that it's fast. In general, PHP always has enough speed to support the dynamic generation of Web content, and many times it is not even possible to find a faster method. However, when faced with massive traffic, high-load applications, limited bandwidth, and other factors that bring performance bottlenecks, you need to consider how to improve PHP performance.
1. Code optimization
Code optimization is not just about writing clean and clear code, but simplifying the code. You can use Zend Optimizer to help you do these tedious tasks. Zend Optimizer can be obtained free of charge from Zend Technologies's website http://www.zend.com/, but must agree to its license agreement, as it is not distributed in GPL form. Its rationale is simple, by detecting the intermediate code produced by the Zend engine and optimizing it to achieve higher execution speed.
After using the Zend Optimizer, the execution efficiency of the complicated PHP source program will be improved immediately, the disadvantage is that the optimized code readability decreases, which makes the code modification difficult.
Zend Optimizer installation method is very simple, just based on the user's platform, download the relevant precompiled version, add the following 2 lines of code to the php.ini file, restart the Web server on the line:
Zend_optimizer.optimizati/>zend_extension=″/path/to/zendoptimizer.so″
Zend_loader.enable=off
The additional third line of code is optional, because disabling Zend_loader will make the optimization faster. It is important to note that Zend_loader can be disabled only if you do not use the Zend Encoder runtime.
2. Using cache
If the size of the PHP program is large, then the way to speed up is to use the cache. There are now a number of cache scenarios to choose from, including Zend Cache, APC, and Afterburner cache.
These are "cache modules" (caching modules). The first time the PHP file is called, the cache module generates some intermediate code from the PHP source code and stores the intermediate code in the Web server's memory. When you call these files later, you can use the in-memory "compiled" code directly. This approach does improve the performance of the application because it minimizes the amount of disk traffic (code has been read and parsed), and the code runs directly in memory, making the server respond to requests much more quickly.
Of course, the cache module also monitors changes in PHP source files and caches pages as necessary to prevent users from getting pages that are still generated by outdated PHP code. Because cache modules can significantly reduce server load and improve the responsiveness of PHP applications, they are ideal for use in heavily loaded websites.
Zend Cache is a commercial software developed by Zend Technologies Corporation. After the first run, the PHP page will run very quickly and the server has more free resources. The disadvantage is that it is not free, but the price is very high.
Afterburner Cache is a free caching module developed by Bware Technologies Corporation. The function is basically the same as Zend cache, but improves performance less than Zend cache.
APC (alternative PHP cache) is another free cache module developed by Community Connect Inc., which is currently available in 2.0.4 and can be obtained from Http://pecl.php.net/package/APC. For product applications, the performance is stable and can greatly increase the speed of response requests.
3. Compress Web Content
There are 1 important factors that affect the speed of access to a site, which is the download speed. The solution is to compress the Web content. For plain text content, HTTP compression technology can compress to less than 40% of the original size, providing more than 60% data transfer savings. Although the Web server will cause a slight increase in CPU usage due to compression, it can save a lot of network IO for transport.
According to the IETF specification, most browsers support content compression using the gzip compression algorithm. That is, you can first use gzip to compress the content of the Web page, and then send to the client browser, the browser will automatically extract the data when it is received, and then display the page. This process is completely transparent to the user. Similarly, there are different ways to compress the contents of a Web page.
Mod_gzip is the 1 open source, Standard Apache module, also known as the Internet Content Accelerator module. It can be compiled with Apache, or it can be used as DSO. It can save around 40% of traffic relative to the normal browsing process. Mod_gzip can not only compress static content, such as HTML, XML, but also on the dynamic generation, including SQL, Java, WML, VRML and other generated content, in the server-side real-time compression and transmission, the compression efficiency is amazing, generally 60%~85%.
Compressing the contents of Dynamic Web pages, you can also use Class.gzip to encode. php files, class.gzip to compress Web content by invoking some of its functions at the beginning and end of a PHP script. If this compression is required for the entire site, these functions can be called in Auto_prepend and Auto_append in the php.ini file, but will take a certain amount of overhead.
PHP4.0.4 introduced 1 new output buffer processing means-ob_gzhandler, its role and class.gzip exactly the same, the difference is can directly add it to the php.ini file, the syntax is as follows:
Output_handler = Ob_gzhandler;
This activates the output buffering capabilities of PHP and compresses the content before it is sent. If you do not want to set up here, only where needed to change the default (not compressed), as long as the need to compress the PHP source program directory, modify the. htaccess file, the syntax is as follows:
Php_value Output_handler Ob_gzhandler
or call it directly in the PHP code:
Ob_start ("Ob_gzhandler");
The output buffering is really good and does not bring additional overhead to the server. One thing to note is that Netscape Communicator does not support image compression. Therefore, you must suppress JPEG and GIF images unless you know that visitors are using Internet Explorer.
4 other tricks
While programming, using some tips can also speed up your PHP operation:
(1) Replace i=i+1 with I+=1, which is in accordance with the habit of C/C + + and relatively higher in efficiency.
(2) Use PHP intrinsic functions whenever possible.
(3) Use single-quote strings whenever possible with single-quote strings. Single-quote strings are more efficient than double-quoted strings.
(4) using foreach instead of the while traversal array, foreach is significantly more efficient than the while loop and does not need to call the Reset function.

The above describes how to improve the speed of PHP run? , including PHP, improve the content, I hope that the PHP tutorial interested in a friend to help.

  • Related Article

    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.