42 Recommendations for PHP code optimization recommended _php Tips

Source: Internet
Author: User
Tags html page memcached php script strlen switch case
1. If a method can be static, make a static declaration on it. The rate can be raised to 4 times times.
2.echo is faster than print.
3. Use Echo's multiple parameters (meaning to use a comma instead of a period) instead of a string connection.
4. Determine the maximum number of loops before executing the For loop, and do not compute the maximum value per loop.
5. Unregister those unused variables, especially large arrays, in order to free memory.
6. Avoid using __get,__set,__autoload as far as possible.
7.require_once () is costly.
8. Using the full path when containing the files will require less time to resolve the operating system path.
9. Using $_server[' request_time ' is better than time () if you want to know when the script starts executing (that is, the server receives the client request).
10. Functions do the same function instead of regular expressions.
The 11.str_replace function is faster than the Preg_replace function, but the STRTR function is four times times more efficient than the Str_replace function.
12. If a string substitution function can accept an array or character as an argument, and the parameter length is not too long, consider writing an extra paragraph of substitution code so that each pass parameter is a character, rather than writing a single line of code to accept the array as a query and replacement parameter.
13. Using the Select Branch statement (ie, switch case) is better than using multiple if,else if statements.
14. Using the @ Block error message is very inefficient.
15. Open the Apache mod_deflate module.
16. The database connection should be turned off when the use is complete.
$row [' ID '] is 7 times times more efficient than $row[id].
18. Error messages are costly.
19. 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.
20. Increments the local variable in the method, the speed is the fastest. Almost as fast as calling a local variable in a function.
21. Incrementing a global variable is twice times slower than incrementing a local variable.
22. Incrementing an object property (such as: $this->prop++) is 3 times times slower than incrementing a local variable.
23. Incrementing an undefined local variable is 9 to 10 times times slower than incrementing a predefined local variable.
24. Defining only a local variable and not calling it in a function also slows down the speed (which is equivalent to incrementing a local variable). PHP will probably check to see if there are any global variables.
25. The method call appears to be independent of the number of methods defined in the class, because I added 10 methods (both before and after the test method), but there was no change in performance.
26. Methods in a derived class run faster than the same method defined in the base class.
27. Call an empty function with one parameter, which takes as much time as 7 to 8 local variable increment operations. A similar method invocation takes approximately 15 times of local variable increments.
28. It is quicker to use single quotes instead of double quotes to contain strings. Because PHP searches for variables in a string enclosed in double quotes, single quotes do not. Of course, you can do this only if you don't need to include variables in the string.
29. When outputting multiple strings, it is faster to separate strings with commas instead of periods. Note: Only echo can do this, it is a "function" that can take multiple strings as arguments: ECHO is the language structure, not the real function, so the function is added double quotes.
30.Apache Parsing a PHP script is 2 to 10 times times slower than parsing a static HTML page. Use static HTML pages as much as possible and use less scripting.
31. Unless the script can be cached, it will be recompiled every time it is invoked. The introduction of a set of PHP caching mechanisms typically increases performance from 25% to 100% to exempt compilation overhead.
32. Try to do the cache, you can use memcached. Memcached is a high-performance memory object caching system that can be used to speed up dynamic Web applications and reduce database load. It is useful to cache the operation Code (OP code) so that the script does not have to recompile for each request.
33. When manipulating strings and having to test whether their lengths meet certain requirements, you will of course use the strlen () function. This function executes fairly quickly because it does not do any calculations and returns only the known string lengths stored in the Zval structure (c's built-in data structure for storing PHP variables). However, since strlen () is a function, it will be somewhat slower, because function calls go through a number of steps, such as lowercase letters, PHP is case-insensitive, and hash lookups follow the functions that are called. In some cases, you can use the isset () technique to speed up the execution of your code.
(Examples below)
if (strlen ($foo) < 5) {echo "Foo is too Short" $$}
(compare 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 () as a language structure means that its execution does not require function lookup and lowercase. That is, you're actually not spending too much on the top-level code that verifies the length of the string.
34. When the execution variable $i is incremented or decremented, $i + + $i slower than the + +. This difference is specific to PHP and does not apply to other languages, so please do not modify your C or Java code and expect them to quickly become useless. + + $i faster because it requires only 3 instructions (opcodes), $i + + requires 4 instructions. A post increment actually produces a temporary variable, which is then incremented. And the predecessor increment increases directly on the original value. This is one of the most optimized processes, as Zend's PHP optimizer has done. Keeping this optimization in mind is a good idea, because not all command optimizer will do the same optimization, and there are a large number of Internet service providers (ISPs) and servers that do not have assembly instruction optimizer.
35. Object-oriented (OOP), object-oriented, often expensive, each method and object invocation consumes a lot of memory.
36. Arrays are also useful not to implement all data structures with classes.
37. Do not subdivide the method too much and think carefully about what code you really intend to reuse.
38. When you need it, you can always break the code into methods.
39. Try to use a large number of PHP built-in functions.
40. If there are a lot of time-consuming functions in your code, you might consider implementing them in C extensions.
41. Evaluation Test (profile) of your code. The inspector will tell you which parts of the code are consuming much of the time. The Xdebug debugger includes a test procedure that can be used to show code bottlenecks in general.
42.mod_zip can be used as an Apache module to instantly compress your data and reduce data transfer by up to 80%.

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.