Some tips for PHP code optimization Summary _php Tutorial

Source: Internet
Author: User
1. If a method is static, make a static declaration of it. The rate of PHP code optimization can be increased to 4 times times.

2.echo is faster than print.

3. Use Echo's multiple parameters (meaning: a comma instead of a period) instead of a string connection.

4. Determine the maximum number of loops before executing the For loop, and do not calculate the maximum value once per loop.

5. Unregister those unused variables, especially large arrays, to free up memory for the purpose of PHP code optimization.

6. Avoid using __get,__set,__autoload as much as possible.

7.require_once () is costly.

8. The full path is used when the file is included, and less time is required to resolve the operating system path.

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.

The 11.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 to implement PHP code optimization.

12. If a string replaces a function that accepts an array or character as an argument, and the parameter length is not too long, consider writing an additional replacement code so that each pass argument is a character instead of just one line of code that accepts the array as a parameter for querying and replacing.

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

14. Masking error messages with @ is very inefficient.

15. Open the Apache mod_deflate module.

16. The database connection should be switched off when it is finished.

The efficiency of the $row [' ID '] is 7 times times that of $row[id].

18. Error messages are costly.

19. Try not to use functions in the For loop, such as for ($x =0; $x < count ($array), $x) the count () function is called every time the loop occurs.

20. Another way to optimize PHP code is to increment the local variable in the method, which is the fastest. is almost equivalent to the speed at which local variables are called in the function.


http://www.bkjia.com/PHPjc/446327.html www.bkjia.com true http://www.bkjia.com/PHPjc/446327.html techarticle 1. If a method is static, make a static declaration of it. The rate of PHP code optimization can be increased to 4 times times. 2.echo is faster than print. 3. Use Echo's multiple parameters (refer to the comma ...

  • 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.