1. It is quicker to use single quotes instead of double quotes to contain strings. Because PHP will search for variables in a string surrounded by double quotes, single quotes will not, note: only echo can do this, it is a string can be used as a parameter of the "function" (in the PHP manual that echo is a language structure, not a real function, so the function with double quotation marks).
2, if the method of the class can be defined as static, as far as possible to define as static, it will increase the speed of nearly 4 times times.
3. The speed of $row [' ID '] is 7 times times that of $row[id].
4. Echo is faster than print, and uses Echo's multiple parameters (referring to commas instead of periods) instead of string connections, such as Echo $str 1, $str 2.
5. To determine the maximum number of cycles before executing a for loop, do not calculate the maximum value once per cycle, preferably using foreach instead.
6. Unregister those unused variables, especially large arrays, in order to free up memory.
7, try to avoid the use of __get,__set,__autoload.
8, require_once () expensive.
9, include files as far as possible to use absolute path, because it avoids PHP to include_path to find the speed of the file, parsing the operating system path takes less time.
10. If you want to know when the script starts executing (the server side receives the client request), use the
$_server[' Request_time '] better than Time ()
11, function instead of regular expression to complete the same function.
12, the Str_replace function is faster than the Preg_replace function, but the efficiency of the STRTR function is four times times the Str_replace function.
13, if a string substitution function, can accept an array or character as a parameter, and the parameter length is not too long, then you can consider an extra piece of replacement code, so that each pass argument is a character, rather than write a line of code to accept the array as the query and replace parameters.
14. Use the Select Branch statement (that is, switch case) better than using multiple if,else if statements.
15. Masking error messages with @ is extremely inefficient and extremely inefficient.
16, open the Apache mod_deflate module, can improve the browsing speed of the page.
17, the database connection should be turned off when used, do not use long connection.
18. Error messages are costly.
19, increment the local variable in the method, the speed is the quickest. is almost equivalent to the speed at which local variables are called in the function.
20, incrementing a global variable is twice times slower than incrementing a local variable.
21, incrementing an object property (such as: $this->prop++) is 3 times times slower than incrementing a local variable.
22. Incrementing an undefined local variable is 9 to 10 times times slower than incrementing a pre-defined local variable.
23. Defining only a 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.
24. 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.
25. A method in a derived class runs faster than the same method defined in the base class.
26. 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.
27, Apache parsing a PHP script time 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.
28. 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.
29, 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.
30. 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.
31. When the increment or decrement of the variable $i is executed, $i + + will be slower $i than + +. 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.
32, not the object-oriented (OOP), object-oriented tends to be very expensive, each method and object calls will consume a lot of memory.
33, not to use the class to implement all the data structure, arrays are also useful.
34, do not subdivide the method too much, think carefully about what you really want to reuse what code?
35, when you need, you can always break down the code into methods.
36, try to use a large number of PHP built-in functions.
37. If there are a lot of time-consuming functions in your code, you might consider implementing them in C extensions.
38. 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.
39, Mod_zip can be used as Apache module, to compress your data instantly, and to reduce the data transfer volume by 80%.
40, in the case can be replaced with file_get_contents file, fopen, feof, Fgets and other series of methods, as far as possible with the file_get_contents, because his efficiency is much higher! But pay attention to file_get_ Contents PHP version problem when opening a URL file;
41, as far as possible to file operations, although PHP file operation efficiency is not low;
42, optimize the Select SQL statement, as far as possible, as little as possible to insert, update operation (on the update, I was a bad batch);
43, as far as possible to use the PHP internal function (but I have to find a php non-existent function, wasted can write a custom function of time, experience problem Ah!) ;
44, the loop inside do not declare variables, especially large variables: objects (this does not seem to be the only thing in PHP to pay attention to it?) ;
45, multidimensional arrays try not to loop nested assignment;
46, in the case of PHP internal string manipulation function, do not use regular expressions;
47, foreach more efficient, try to use foreach instead of while and for loop;
48. Use single quotation marks instead of double quotation marks to reference strings;
49, "Replace I=i+1 with I+=1." In line with C + + habits, efficiency is high ";
50, the global variable, should be used up on unset () off.
51, using CHECKDNSRR () through the existence of the domain name to confirm the validity of some email address, this built-in function can ensure that each domain name corresponds to an IP address;
52, if you are using PHP5 and mysql4.1 above version, consider using Mysql_ modified function Mysqli_;
53, using Highlight_file () can automatically print a good format of the page source code copy;
54. Use the error_reporting (0) function to prevent potentially sensitive information from being displayed to the user. The ideal error report should be completely disabled in the php.ini file. However, if you are using a shared virtual host, php.ini you can not modify, then you'd better add error_reporting (0) function, put in the first line of each script file (or with require_once () to load) This effectively protects sensitive SQL queries and paths from being displayed when errors occur;
55. Use the error_reporting (0) function to prevent potentially sensitive information from being displayed to the user. The ideal error report should be completely disabled in the php.ini file. However, if you are using a shared virtual host, php.ini you can not modify, then you'd better add error_reporting (0) function, put in the first line of each script file (or with require_once () to load) This effectively protects sensitive SQL queries and paths from being displayed when errors occur;
56. A function has multiple return values through a reference to the argument variable address. You can add a "&" to the variable before it is passed by address rather than by value;
57, turn off the PHP magic quotation mark (Magic Quote) function, not every part of the escaped data to be inserted into the database, if all the data into PHP is escaped, it will have a certain effect on the execution efficiency of the program. Calling escape functions at run time (such as addslashes ()) is more efficient
58. Use the Ip2long () and LONG2IP () functions to convert the IP address into an integer type into the database instead of the character type. This reduces storage space by almost 1/4. At the same time, the address can be easily sorted and quickly find;
59 . Use Gzcompress () and gzuncompress () to compress (decompress) the large-capacity string when the database is stored in (out). This built-in function can be compressed to 90% using the GZIP algorithm;
60. use CHECKDNSRR () to confirm the validity of some email addresses through the presence of a domain name. This built-in function ensures that each domain name corresponds to an IP address.
61. some places use isset instead of strlen
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.
50 tips for improving PHP execution efficiency