53 key points to improve PHP programming efficiency

Source: Internet
Author: User
Tags define array variables php programming switch case variable

It is quicker to use single quotes instead of double quotes to contain strings. Because PHP searches for variables in a string enclosed in double quotes, single quotes are not, note: only echo can do this, it is a "function" that can take multiple strings as arguments: ECHO is the language structure, not the real function, so the function is added double quotes.

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

2, $row [' ID '] speed is $row[id] 7 times times.

3, ECHO is faster than print, and uses Echo's multiple arguments (to refer to a comma rather than a period) instead of a string connection, such as Echo $str 1, $str 2.

4, before executing the For loop to determine the maximum number of loops, do not calculate the maximum value per cycle, preferably using foreach instead.

5, the cancellation of those unused variables, especially large arrays, in order to free memory.

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

7, require_once () costly.

8, include files as far as possible using the absolute path, because it avoids the PHP to include_path to find the file speed, parsing the operating system path requires less time.

9, if you want to know the script to start execution (that is, the server to receive client requests) at the moment, using $_server[' request_time ' is better than time ().

10. Functions do the same function instead of regular expressions.

11, the Str_replace function is faster than the Preg_replace function, but the efficiency of the STRTR function is four times times that of 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, 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.

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

14, using the @ block error message is very inefficient, extremely inefficient.

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

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

17. Error messages are costly.

18, in the method to increase the local variable, speed is the fastest. Almost as fast as calling a local variable in a 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.




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.