43 Recommendations for optimizing PHP code (recommended) _php tutorial

Source: Internet
Author: User
Tags switch case
1. If a method can be static, declare it static. Speed improvement are by a factor of 4. If a method is static, it is statically declared. The rate can be increased to 4 times times.

2. Echo is faster than print. echo is faster than print.

3. Use echo ' s multiple parameters instead of string concatenation. Use Echo's multiple arguments, which refer to a comma instead of a period, instead of a string connection.

4. Set the MaxValue for your for-loops before and not in the loop. Determine the maximum number of loops before executing the For loop, and do not calculate the maximum value once per loop.

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

6. Avoid Magic like __get, __set, __autoload try to avoid using __get,__set,__autoload.

7. Require_once () is expensive require_once () expensive.

8. Use full paths in includes and requires, less time spent on resolving the OS paths. The full path is used when the file is included, and less time is required to resolve the operating system path.

9. If you need to find out the time time of the script started executing, $_server[' request_time '] is preferred to time () Want to know when the script starts executing (that is, the server side receives the client request) the moment, uses $_server[' Request_time '] is better than time ().

See if you can use STRNCASECMP, strpbrk and Stripos instead of regex. Check if you can use the Strncasecmp,strpbrk,stripos function instead of the regular expression to accomplish the same function.

One. Str_replace is faster than preg_replace, but strtr are faster than str_replace by a factor of 4. 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.

If the function, such as String replacement function, accepts both arrays and single characters as arguments, and If y Our argument list isn't too long, consider writing a few redundant replacement statements, passing one character at a Tim E, instead of one line of code, accepts arrays as search and replace arguments. 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.

It ' s better to use SELECT statements than multi if, else if, statements. It is better to use multiple if,else if statements using the Select Branch statement (that is, switch case).

Error suppression with @ is very slow. Using @ To mask error messages is very inefficient.

Turn on Apache's mod_deflate open the Apache mod_deflate module.

The. Close your database connections when you ' re-done with them. The database connection should be turned off when it is finished.

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

Error messages is expensive. Error messages are costly.

Functions inside of for loops, such as for ($x =0; $x < count ($array); $x) the count () function gets Calle D each time. Try not to use functions in the For loop, such as for ($x =0; $x < count ($array), $x) call the count () function once per loop.

    • Total 2 Pages:
    • Previous page
    • 1
    • 2
    • Next page

http://www.bkjia.com/PHPjc/364246.html www.bkjia.com true http://www.bkjia.com/PHPjc/364246.html techarticle 1. If a method can be static, declare it static. Speed improvement are by a factor of 4. If a method is static, it is statically declared. The rate can be increased to 4 times times. 2. Ech ...

  • Related Article

    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.