42 PHP code optimization suggestions. This article introduces 42 suggestions for optimizing PHP code: 1. if a method can be static, it will be declared as static. The speed can be increased to 4 times. 2. echo is faster than print. 3. using multiple parameters of echo this article describes 42 suggestions for optimizing PHP code:
1. if a method can be static, it will be declared as static. The 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. cancel unnecessary variables, especially large arrays, to release the memory.
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 (I .e. when the SERVER receives a client REQUEST), use $ _ SERVER ['request _ time'] instead of time ().
10. check whether strncasecmp, strpbrk, and stripos functions can replace regular expressions to implement the same function.
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.
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 (note: switch case) 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. for example, for ($ x = 0; $ x <count ($ array); $ x) calls count () once every loop () function.
20. increase the local variable in the method at the fastest speed. It is almost the same as calling a local variable in a function.
21. increasing a global variable is twice slower than increasing a local variable.
22. incrementing an object property (for example, $ this-> prop) is three times slower than incrementing a local variable.
23. increasing an unspecified local variable is 9 to 10 times slower than increasing a predefined local variable.
24. defining only one local variable without calling it in the function also slows down (to the extent that it is equivalent to increasing a local variable ). PHP will probably check whether global variables exist.
25. method calling seems to have nothing to do with the number of methods defined in the class, because I have added 10 methods before and after the test method, but the performance has not changed.
26. the method in the derived class runs faster than the same method defined in the base class.
27. calling an empty function with a parameter takes seven to eight times to increment local variables. Similar method calls take nearly 15 times to increment local variables.
28. replace double quotation marks with single quotes to include strings, which is faster. Because PHP searches for variables in strings enclosed by double quotation marks, but not in single quotes. Of course, this can be done only when you do not need to include a variable in a string.
29. when multiple strings are output, use commas instead of periods to separate strings, which is faster. Note: Only echo can do this. it is a "function" that can treat multiple strings as parameters, so the function is added with double quotation marks ).
30. Apache parses a PHP script two to ten times slower than parsing a static HTML page. Use static HTML pages and less scripts as much as possible.
31. unless the script can be cached, it will be re-compiled every time it is called. The introduction of a PHP Cache mechanism can generally improve the performance by 25% to 100%, so as to avoid compilation overhead.
32. use memcached as much as possible. Memcached is a high-performance memory object cache system that can accelerate dynamic Web applications and reduce database load. It is useful for the cache of OP code, so that the script does not have to re-compile each request.
33. when operating a string and checking whether its length meets certain requirements, you will use the strlen () function. This function is executed quite quickly because it does not perform any calculations and only returns the known string length stored in the zval structure (C's built-in data structure, used to store PHP variables. However, because strlen () is a function, it is more or less slow, because function calls take many steps, such as lowercase letters, PHP does not distinguish between case-insensitive function names.) and hash searches are executed along with the called function. In some cases, you can use the isset () technique to accelerate your code execution.
Ex. (for example)
If (strlen ($ foo) <5) {echo "Foo is too short ";}
Vs. (compared with the following tips)
If (! Isset ($ foo {5}) {echo "Foo is too short ";}
Calling isset () happens to be faster than strlen (), because unlike the latter, isset () is used as a language structure, this means that function search and lowercase letters are not required for execution. That is to say, in fact, you do not spend too much money in the top-level code that checks the string length.
34. when the execution variable $ I increments or decreases, $ I is slower than $ I. This difference is exclusive to PHP and does not apply to other languages. therefore, do not modify your C or Java code and expect them to become faster and useless immediately. $ I is faster because it only requires three commands (opcodes), and $ I requires four commands. In fact, a temporary variable is generated in post-increment mode, which is then incremented. The pre-increment directly increases on the original value. This is a kind of optimization, as the Zend PHP optimizer does. Keeping this optimization processing in mind is a good idea, because not all command optimizers perform the same optimization and there are a large number of Internet service providers (ISPs) that do not have command optimizers) and server.
35. object-oriented (OOP) is not required. object-oriented usually has a high overhead, and each method and object call consumes a lot of memory.
36. arrays are also useful instead of using classes to implement all data structures.
37. do not subdivide the methods too much. think carefully about the code you actually intend to reuse?
38. when you need it, you can always break down the code into methods.
39. try to use a large number of PHP built-in functions.
40. if there are a large number of time-consuming functions in the code, you can consider using C extension to implement them.
41. profile your code. The validator will tell you how much time the code consumes. The Xdebug debugger contains an inspection program, which can display the code bottleneck in general.
42. mod_zip can be used as an Apache module to instantly compress your data and reduce the data transmission volume by 80%.
Lifecycle 1. if a method can be static, it will be declared as static. The speed can be increased to 4 times. 2. echo is faster than print. 3. use multiple parameters of echo...