Summary of PHP Optimizations

Source: Internet
Author: User

1. Echo is faster than print, and uses Echo's multiple parameters (referring to commas instead of periods) in place of string connections, such as Echo $str 1, $str 2. If you use Echo $str 1. $STR 2 will require the PHP engine to first connect all the variables, and then in the output, and Echo $str 1, $str the 2,php engine will sequentially output their

2. Foreach is more efficient and use foreach instead of while and for loops as much as possible. To determine the maximum number of loops before executing a for loop, do not calculate the maximum value once per loop, preferably using foreach instead. (while loops are more than 15 times times more efficient than for-loop execution, and Foreach loops are For 120 times times faster than loops)

3, require_once () expensive. Require_once and include_once need to be weighed, so the efficiency is low, but after the 5.2 version of the efficiency problem has been basically solved.

4. Unregister those unused variables, especially large arrays, in order to free up memory.

5, the global variable, should be used up on unset () off.

6, include files as far as possible to use absolute path, because it avoids PHP to include_path to find the speed of the file, parsing the operating system path takes less time. (use less iniset () to set include_path)

7. If you want to know when the script starts executing (that is, the server side receives the client request), use $_server[' request_time '] better than time ().  ($_server[' request_time ') saves the timestamp at the time the request was initiated, and time () returns the Unix timestamp at the current moment. )

8, incrementing a global variable is twice times slower than incrementing a local variable.

9, Apache parsing a PHP script time is 2 to 10 times times slower than parsing a static HTML page. Use static HTML pages as much as possible and use fewer scripts

10. Unless the script can be cached, it will be recompiled every time it is called. The introduction of a set of PHP caching mechanisms can generally improve performance of 25% to 100%, to eliminate the compilation overhead, so as far as possible cache, you can use memcached. The memcached is a high-performance memory object caching system that can be used to accelerate dynamic Web applications and reduce database load. Caching of the OP code is useful so that the script does not have to recompile for each request.

11, when manipulating a string and need to verify that its length satisfies a certain requirement, the use of the strlen () function executes quite quickly, because it does not do any calculations, only returns the length of the known string stored in the Zval structure (the built-in data structure of C for storing PHP variables). However, because strlen () is a function, it is somewhat slower because function calls go through a number of steps, such as lowercase letters (lowercase functions, php not distinguishing between function names), and hash lookups, which are executed in conjunction with the call. In some cases, you can use the isset () technique to speed up execution of your code, because unlike strlen (), Isset (), as a language structure, means that it does not require function lookups and lowercase letters. In other words, you're actually not spending much overhead in the top-level code that examines the length of the string.

12, when the execution variable $i increment or decrement, $i + + will be slower than the + + $i, this difference is unique to PHP. + + $i is faster because it requires only 3 instructions (opcodes), $i + + requires 4 instructions. The post increment actually produces a temporary variable, which is then incremented. The predecessor increment is incremented directly on the original value. This is one of the most optimized processing, as Zend's PHP optimizer did.

13, optimize the Select SQL statement, as far as possible, as little as possible to insert, update operation

Summary of PHP Optimizations

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.