53 Tips to Improve PHP Programming Efficiency

This article is sourced from the internet. Some of the standards are not universally agreed upon, so just take it as a reference!

1. If a class method can be defined as static, do it. Its speed will improve nearly 4 times.

2. $row[’id’] is 7 times faster than $row[id].
3. echo is faster than print, and use echo’s multiple parameters (note: separated by commas, not periods) instead of string concatenation, e.g., echo $str1,$str2.
4. Determine the maximum loop count before executing a for loop; do not recalculate the maximum value every iteration. It’s better to use foreach instead.
5. Unset unused variables, especially large arrays, to free up memory.
6. Try to avoid using __get, __set, and __autoload.
7. require_once() is expensive.
8. Use absolute paths when including files, as it avoids the time PHP spends searching through include_path, reducing the time needed to resolve the operating system path.
9. If you need to know when a script started executing (i.e., when the server received the client request), use $_SERVER[‘REQUEST_TIME’] instead of time().
10. Use functions instead of regular expressions to achieve the same functionality.
11. The str_replace function is faster than preg_replace, but strtr is four times more efficient than str_replace.
12. If a string replacement function can accept an array or character as a parameter and the parameter length is not too long, consider writing an extra piece of replacement code that passes a single character per call, rather than writing a single line of code that accepts arrays as search and replace parameters.
13. Using a switch statement (switch case) is better than using multiple if and else if statements.
14. Using @ to suppress error messages is very inefficient, extremely inefficient.
15. Enable Apache’s mod_deflate module to improve web page browsing speed.
16. Close database connections when they are no longer needed; do not use persistent connections.
17. Error messages are expensive.
18. Incrementing local variables within a method is the fastest. Its speed is almost equivalent to calling local variables within a function.
19. Incrementing a global variable is 2 times slower than incrementing a local variable.
20. Incrementing an object property (e.g., $this->prop++) is 3 times slower than incrementing a local variable.
21. Incrementing an undefined local variable is 9 to 10 times slower than incrementing a predefined local variable.
22. Simply defining a local variable without calling it within a function will still slow things down (to an extent comparable to incrementing a local variable). PHP likely checks to see if a global variable exists.
23. Method calls appear to be independent of the number of methods defined in a class, because I added 10 methods (both before and after testing) and saw no change in performance.
24. Methods in a derived class run faster than the same methods defined in a base class.
25. Calling an empty function with one parameter takes about the same time as performing 7 to 8 local variable increment operations. A similar method call takes about the time equivalent to 15 local variable increment operations.
26. Apache parses a PHP script 2 to 10 times slower than a static HTML page. Use more static HTML pages and fewer scripts.
27. Unless a script can be cached, it will be recompiled on every call. Introducing a PHP caching mechanism can typically improve performance by 25% to 100% by eliminating compilation overhead.
28. Do as much caching as possible; use memcached. Memcached is a high-performance, in-memory object caching system that can accelerate dynamic web applications and reduce database load. Caching Opcodes (OP codes) is very useful, as it prevents scripts from needing to be recompiled for each request.
29. When manipulating strings and needing to verify if their length meets a requirement, you would naturally think to use the strlen() function. This function executes fairly quickly because it performs no calculation, only returning the known string length stored in the zval structure (C’s built-in data structure used for storing PHP

variables). However, since strlen() is a function, it is somewhat slow because function calls go through many steps, such as lowercasing (the function name) and hash lookups, which execute along with the called function. In some cases, you can use the isset() trick to speed up your code execution.
(Example below)
if (strlen($foo) < 5) { echo “Foo is too short”$$ }
(Compare with the following trick)
if (!isset($foo{5})) { echo “Foo is too short”$$ }
Calling isset() happens to be faster than strlen() because, unlike the latter, isset() is a language construct, meaning its execution does not require function lookup and lowercasing. In other words, you don’t incur much overhead in the top-level code that checks string length.
34. When incrementing or decrementing a variable $i, $i++ is slightly slower than ++$i. This difference is specific to PHP and does not apply to other languages, so please do not modify your C or Java code expecting it to become faster immediately鈥攊t won’t work. ++$i is faster because it requires only 3 instructions (opcodes), whereas $i++ requires 4. Post-increment actually creates a temporary variable, which is then incremented. Pre-increment increments the original value directly. This is a type of optimization, just like what Zend’s PHP optimizer does. Keeping this optimization in mind is a good idea, because not all instruction optimizers perform the same optimization, and many Internet Service Providers (ISPs) and servers do not have instruction optimizers installed.
35. Not everything needs to be Object-Oriented (OOP); OOP often incurs significant overhead, with every method and object call consuming a lot of memory.
36. You don’t have to use classes to implement all data structures; arrays are also very useful.
37. Don’t subdivide methods too finely; carefully consider which code you truly intend to reuse.
38. You can always break code down into methods when you need to.
39. Try to utilize a large number of PHP’s built-in functions.
40. If your code contains many time-consuming functions, you might consider implementing them as C extensions.
41. Profile your code. A profiler will tell you which parts of your code consume how much time. The Xdebug debugger includes a profiler; profiling generally reveals the bottlenecks in your code.
42. mod_zip can be used as an Apache module to compress your data on-the-fly and reduce data transfer volume by 80%.
43. When you can use file_get_contents to replace the

Leave a Comment

Your email address will not be published.