Home > Article > Backend Development > Forty-three suggestions for optimizing PHP code (recommended)_PHP Tutorial
1. If a method can be static, declare it static. Speed improvement is by a factor of 4. If a method can be static, declare it static. 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 echo’s multiple parameters instead of string concatenation.
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 every time you loop.
5. Unset your variables to free memory, especially large arrays. Unset your variables, especially large arrays, 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() is expensive.
8. Use full paths in includes and requires, less time spent on resolving the OS paths.
9. If you need to find out the time when the script started executing, $_SERVER['REQUEST_TIME'] is preferred to time() ), it is better to use $_SERVER['REQUEST_TIME'] than time().
10. See if you can use strncasecmp, strpbrk and stripos instead of regex. Check if you can use strncasecmp, strpbrk and stripos functions instead 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. str_replace function is faster than preg_replace function, but strtr function is four times more efficient than 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 accepts arrays or characters as parameters, and the parameter length is not too long, then you can consider writing an additional replacement code so that the parameters are passed every time is a character instead of just writing a line of code that accepts arrays as parameters for query and replace.
13. It’s better to use select statements than multi if, else if, statements.
14. Error suppression with @ is very slow. Using @ to block error messages is very inefficient.
15. Turn on apache’s mod_deflate Turn on apache’s mod_deflate module.
16. Close your database connections when you’re done with them. Database connections should be closed when you are done using them.
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. Error messages are 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. Try not to use functions inside for Functions are used in loops, such as for ($x=0; $x < count($array); $x), which calls the count() function every time it loops.