PHP's unique syntax mixes C, Java, Perl, and PHP's self-innovative syntax. It can perform dynamic Web pages more quickly than CGI or Perl. Dynamic pages made with PHP are embedded in HTML documents to execute, compared to other programming languages, and are much more efficient than CGI, which fully generates HTML markup. The optimization guidelines for 42 programs are described below.
1. If a method is static, make a static declaration of it. The rate can be increased to 4 times times.
2.echo is faster than print.
3. Use Echo's multiple parameters (meaning: 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 calculate the maximum value once per loop.
5. Unregister those unused variables, especially large arrays, in order to free up memory.
6. Avoid using __get,__set,__autoload as much as possible.
7.require_once () is costly.
8. The full path is used when the file is included, and less time is required to resolve the operating system path.
9. If you want to know when the script starts executing (that is, the server side receives the client request), use $_server[' request_time '] better than time ().
10. function instead of regular expression to complete the same function.
The 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 replaces a function that accepts an array or character as an argument, and the parameter length is not too long, consider writing an additional replacement code so that each pass argument is a character instead of just one line of code that accepts the array as a parameter for querying and replacing.
13. Use the Select Branch statement (that is, switch case) better than using multiple if,else if statements.
14. Masking error messages with @ is very inefficient.
15. Open the Apache mod_deflate module.
16. The database connection should be switched off when it is finished.
The efficiency of the $row [' ID '] is 7 times times that of $row[id].
18. Error messages are costly.
19. Try not to use functions in the For loop, such as for ($x =0; $x prop++) 3 times times slower than incrementing a local variable.
23. Incrementing a non-predefined local variable is 9 to 10 times times slower than incrementing one of the pre-defined local variables.
24. Defining only one local variable without calling it in the function also slows down the speed (which is equivalent to incrementing a local variable). PHP will probably check to see if there are global variables.
25. The method invocation appears to be independent of the number of methods defined in the class, because I added 10 methods before and after the test method, but there was no performance change.
26. A method in a derived class runs faster than the same method defined in the base class.
27. Call an empty function with one parameter, which takes the same amount of time as performing a local variable increment operation of 7 to 8 times. A similar method call takes a time close to 15 local variable increment operations.
28. It is quicker to use single quotes instead of double quotes to contain strings. Because PHP searches for a variable in a string surrounded by double quotes, the single quotation mark does not. Of course, you can do this only if you don't need to include variables in the string.
29. When outputting multiple strings, use commas instead of periods to separate strings, faster. Note: Only echo can do this, it is a "function" that can take multiple strings as arguments (the PHP manual says that ECHO is a language structure, not a real function, so the function is enclosed in 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 fewer scripts.
31. Unless the script can be cached, it will be recompiled every time it is called. Introducing a set of PHP caching mechanisms can typically improve performance by 25% to 100% to eliminate compilation overhead.
32. Try to do the cache, you can use memcached. The memcached is a high-performance memory object caching system that can be used to accelerate dynamic Web applications and reduce database load. Caching of the OP code is useful so that the script does not have to recompile for each request.
33. When you manipulate a string and need to verify that its length satisfies a certain requirement, you will of course use the strlen () function. This function executes quite quickly because it does not do any calculations and only returns the length of the known string stored in the Zval structure (the built-in data structure of C for storing PHP variables).
However, because strlen () is a function, it is somewhat slower, because function calls go through a number of steps, such as lowercase letters (lowercase, PHP does not differentiate between function names), and hash lookups, which follow the called functions. In some cases, you can use the isset () technique to speed up execution of your code.
(Example 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 lookups and lowercase letters. In other words, you're actually not spending much overhead in the top-level code that examines the length of the string.
34. When the variable $i is incremented or decremented, $i + + will be slower $i than the + +. This difference is unique to PHP and does not apply to other languages, so please do not modify your C or Java code and expect them to become fast and useless. + + $i is faster because it requires only 3 instructions (opcodes), $i + + requires 4 instructions. The post increment actually produces a temporary variable, which is then incremented. The predecessor increment is incremented directly on the original value. This is one of the most optimized processing, as Zend's PHP optimizer did.
It is a good idea to keep this optimization in mind, as not all of the instruction optimizer will do the same optimizations, and there are a large number of Internet service providers (ISPs) and servers that do not have the command optimizer installed.
35. Not everything has to be object-oriented (OOP), object-oriented is often expensive, and each method and object invocation consumes a lot of memory.
36. Not all data structures are implemented with classes, and arrays are useful.
37. Don't subdivide the method too much, think carefully about what code you really plan to reuse?
38. When you need to, 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 lot of time-consuming functions in your code, you might consider implementing them in C extensions.
41. Evaluate the test (profile) of your code. The inspector will tell you what parts of the code are consuming time. The Xdebug debugger contains an inspection program that can show the bottleneck of the code in general.
The 42.mod_zip can be used as an Apache module to instantly compress your data and reduce data transfer by up to 80%.
42 optimization Criteria that should be noted by the PHP development program