Introduction to 21 Ways to improve PHP performance

Source: Internet
Author: User
Tags switch case

1. Using single quotes to contain strings is faster than double quotes to contain strings. Because PHP searches for variables in a string enclosed in double quotes, single quotes do not.
2. If you can define the method of a class as static, try to define it as static, and it will increase by nearly 4 times times.
3. $row [' ID '] is 7 times times the speed of $row[id].
4.echo is faster than print, and uses Echo's multiple arguments (to refer to a comma instead of a period) instead of a string connection, such as Echo $str 1, $str 2.
5. Determine the maximum number of loops before performing a For loop, and do not compute the maximum value per loop, preferably by using foreach instead.
6. Unregister those unused variables, especially large arrays, to free up memory.
7. Try to avoid using __get,__set,__autoload. The
8.require_once () is costly.
9.include files use absolute paths as much as possible, because it avoids the speed with which PHP looks for files in Include_path, and it takes less time to parse the operating system path.
10. The use of $_server[' request_time ' is better than time () if you want to know when the script starts executing (that is, the server receives the client request).
11. Functions do the same function instead of regular expressions. The
12.str_replace function is faster than the Preg_replace function, but the STRTR function is four times times more efficient than the Str_replace function.
13. If a string substitution function can accept an array or character as a parameter, and the parameter length is not too long, consider writing an extra paragraph of substitution code so that each pass parameter is a character, rather than writing a single line of code to accept the array as a query and replacement parameter.
14. Using the Select Branch statement (switch case) is better than using multiple if,else if statements.
15. The practice of using @ To block error messages is very inefficient and extremely inefficient.
16. Open the Apache Mod_deflate module to improve the browsing speed of the Web page.
17. Database connection should be turned off when finished, do not use long connections.
18. Error messages are costly.
19. Increments the local variable in the method, the speed is the fastest. Almost as fast as calling a local variable in a function.
20. Incrementing a global variable is twice times slower than incrementing a local variable.
21. Incrementing an object property (such as: $this->prop++) is 3 times times slower than incrementing a local variable.

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.