Some key points for PHP newcomers to quickly improve program efficiency

Source: Internet
Author: User
Tags switch case
Execution efficiency is a benchmark for measuring code quality, and there are many aspects of optimizing execution efficiency. Today we will explain how to optimize the execution efficiency of our programs from the programming details.

Using single quotes instead of double quotation marks to contain strings can be faster. Because PHP will search for variables in a string surrounded by double quotes, single quotes will not, note: only echo can do this, it is a string can be used as a parameter of the "function" (in the PHP manual that echo is a language structure, not a real function, so the function with double quotation marks).

1, if the method of the class can be defined as static, as far as possible to define as static, it will increase the speed of nearly 4 times times.

2. The speed of $row [' ID '] is 7 times times that of $row[id].

3. Echo is faster than print, and uses Echo's multiple parameters (referring to commas instead of periods) instead of string connections, such as Echo $str 1, $str 2.

4, determine the number of * cycle before executing the For loop, do not calculate * Large value at once per loop, * Good use of foreach instead.

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

6, try to avoid the use of __get,__set,__autoload.

7, require_once () expensive.

8, 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.

9. 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 ().

10, function instead of regular expression to complete the same function.

11, the Str_replace function is faster than the Preg_replace function, but the efficiency of the STRTR function is four times times the Str_replace function.

12, if a string substitution function, can accept an array or character as a parameter, and the parameter length is not too long, then you can consider an extra piece of replacement code, so that each pass argument is a character, rather than write a line of code to accept the array as the query and replace parameters.

13. Use the Select Branch statement (that is, switch case) better than using multiple if,else if statements.

14. Masking error messages with @ is extremely inefficient and extremely inefficient.

15, open the Apache mod_deflate module, can improve the browsing speed of the page.

16, the database connection should be turned off when used, do not use long connection.

17. Error messages are costly.

18, increment the local variable in the method, the speed is * fast. is almost equivalent to the speed at which local variables are called in the function.

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

20, incrementing an object property (such as: $this->prop++) is 3 times times slower than incrementing a local variable.

Related recommendations:

PHP performance analysis and experimentation--a macro analysis of performance 1

PHP performance Analysis and experimentation: a microscopic analysis of performance

How do I use tools for online PHP performance tracking and analysis?

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.