Forty-three suggestions for optimizing PHP code (recommended ). 1. Ifamethodcanbestatic, declareitstatic. Speedimprovementisbyafactorof4. if a method can be static, it will be declared as static. The speed can be increased to 4 times. 2. ech 1. If a method can be static, declare it static. Speed improvement is by a factor of 4. If a method can be static, make a static declaration on it. The speed can be increased to 4 times.
2. echo is faster than print. echo is faster than print.
3. Use echo's multiple parameters instead of string concatenation. Use multiple parameters of echo to replace string connections with commas instead of periods.
4. Set the maxvalue for your for-loops before and not in the loop. determine the maximum number of cycles before executing the for loop. do not calculate the maximum value for each loop.
5. Unset your variables to free memory, especially large arrays. cancel unnecessary variables, especially large arrays, to release the memory.
6. Avoid magic like _ get, _ set, _ autoload Avoid using _ get ,__ set ,__ autoload whenever possible.
7. require_once () is expensive.
8. Use full paths in between DES and requires, less time spent on resolving the OS paths. Use the full path when the file is included, and less time is required to parse the operating system path.
9. if you need to find out the time when the script started executing, $ _ SERVER ['request _ time'] is preferred to TIME () If you want to know how to execute the script: that is, when the SERVER receives a client REQUEST, the use of $ _ SERVER ['request _ time'] is better than the TIME ().
10. See if you can use strncasecmp, strpbrk and stripos instead of regex. check whether strncasecmp, strpbrk, and stripos functions can be used in place of regular expressions to complete the same function.
11. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4. 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.
12. if the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments. 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 The code accepts arrays as the query and replacement parameters.
13. It's better to use select statements than multi if, else if, statements. Using the select branch statement (: switch case) is better than using multiple if and else if statements.
14. Error suppression with @ is very slow. blocking Error messages with @ is very inefficient.
15. Turn on apache's mod_deflate to open apache's mod_deflate module.
16. Close your database connections when you're done with them. the database connection should be closed after use.
17. $ row ['id'] is 7 times faster than $ row [id]. $ row ['id'] is 7 times more efficient than $ row [id.
18. Error messages are expensive. the Error message is expensive.
19. do not use functions inside of for loop, such as for ($ x = 0; $ x <count ($ array); $ x) The count () function gets called each time. 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.
- 2 pages in total:
- Previous Page
- 1
- 2
- Next page
Http://www.bkjia.com/PHPjc/364246.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/364246.htmlTechArticle1. If a method can be static, declare it static. Speed improvement is by a factor of 4. If a method can be static, make a static declaration on it. The speed can be increased to 4 times. 2. ech...