PHP code optimization tips _ PHP Tutorial

Source: Internet
Author: User
PHP code optimization tips. 1. if a method can be static, it will be declared as static. PHP code optimization speed can be increased to 4 times. 2. echo is faster than print. 3. use multiple echo parameters1. if a method can be static, it will be declared as static. PHP code optimization speed can be increased to 4 times.

2. echo is faster than print.

3. use multiple echo parameters instead of periods to replace string connections.

4. determine the maximum number of cycles before executing the for loop. do not calculate the maximum value for each loop.

5. unregister unnecessary variables, especially large arrays, to release the memory for PHP code optimization.

6. avoid using _ get ,__ set ,__ autoload whenever possible.

7. require_once () is expensive.

8. if you use the full path when the file is included, it takes less time to parse the operating system path.

9. if you want to know the TIME when the script starts to be executed (the SERVER receives the client REQUEST), use $ _ SERVER ['request _ time'] instead of time ().

10. functions use the same functions instead of regular expressions.

11. the str_replace function is faster than the preg_replace function, but the strtr function is four times more efficient than the str_replace function to achieve PHP code optimization.

12. if a string replacement function can take an array or character as a parameter, and the parameter length is not too long, you can consider writing an additional replacement code so that each parameter passing is a character, instead of writing only one line of code to accept arrays as query and replacement parameters.

13. Using the select branch statement is better than using multiple if and else if statements.

14. blocking error messages with @ is very inefficient.

15. open the mod_deflate module of apache.

16. the database connection should be closed after use.

17. $ row ['id'] is 7 times the efficiency of $ row [id.

18. the error message is expensive.

19. do not use functions in a for loop, such as for ($ x = 0; $ x <count ($ array); $ x). the count () function is called once every loop.

20. Another PHP code optimization method is to increase the local variable in the method, and the speed is the fastest. It is almost the same as calling a local variable in a function.


Bytes. PHP code optimization speed can be increased to 4 times. 2. echo is faster than print. 3. use multiple echo parameters...

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.