PHP efficient written

0, with single quotes instead of double quotes to contain the string, this will faster. Because PHP will be surrounded by double quotation marks to search for a string variable, single quotes will not, pay attention: only the echo can do this, it is a parameter can take several strings as the "function" (Yi Zhu: PHP Manual said echo is a language construct, not a real function, so the function with the double quotes).
1, if the method of the class can define static, as defined on the static, its speed will increase nearly four-fold.

2, $ row ['id'] rate is $ row [id] of 7 times.

3, echo faster than print, and use the echo of multiple parameters (Yi Zhu: refers to a comma instead of dot) instead of string concatenation, such as echo $ str1, $ str2.

4, in the implementation of the for loop to determine the maximum number of cycles before, not in the loop had to be calculated once the maximum value, it is best to replace the use of foreach.

5 Unset variables, especially large arrays to free memory.

6, try to avoid using __get, __set, __autoload.

7, require_once () are expensive.

8, include file, try to use an absolute path, because it avoids the include_path in PHP to find the document, the time required for resolving the OS paths will be less.

9, if you want to know the script started executing (Yi Zhu: the client requests the server received) time, using $ _SERVER ['REQUEST_TIME'] is better than the time ().

10, the function instead of a regular expression to accomplish the same function.

11, str_replace function function faster than preg_replace, but strtr function of the efficiency is four times the str_replace function.

12, if a string replace function that accepts an array or character as a parameter, and the parameter length is not too long, you can consider additional write a replacement code, so each passing parameter is a character, rather than just write a single line of code that accepts an array as the query and replace arguments.

13, using the select statements (Yi Zhu: a switch case) better than multiple if, else if statements.

14 Error suppression with @ is very slow, very inefficient.

15, open apache's mod_deflate module, can improve the web browsing speed.

16, the database connection when you're done turn off, do not use long connection.

17, the error messages are expensive.

18, the method of increasing local variable, speed is the fastest. Almost in a function local variable in a.

19, increment a global variable than a local variable is 2 times slower.

20, Incrementing an object property (eg: $ this-> prop + +) slower than a local variable 3 times.

21, incrementing a local variable is not predefined increments than a predefined local variable 9 to 10 times slower.

22, only define a local variable and not in a function call it, it will also slow down the speed (the same amount as incrementing a local variable). PHP probably does a check to see if the global exists.

23, method invocation appears to be the methods defined in class independent of the number, because I (in the test method before and after) added 10 more methods, but no change in performance.

24, the derived class method run faster in the base class defined in the same way.

25, called with a parameter and an empty function body takes time to implement the equivalent of 7 to 8 times the local variable increment operation. A similar method call time spent close to 15 times the local variable increment operation.

26, Apache parsing a PHP script time than a static HTML page 2 to 10 times slower. Try to use more static HTML pages and fewer scripts.

27, unless the script can be cached, otherwise the call will be recompiled every time. Introduction of a PHP caching product to typically increase from 25 to 100 percent performance by removing compile overhead.

28, as do the cache, use memcached. memcached is a high-performance memory object caching system to speed up dynamic Web applications by alleviating database load. On the operation code (OP code) caches are useful so that your script does not recompile on every request.

29, when the operation of string and you need to check a certain length requirements, assuming you will use strlen () function. This function is pretty quick, because it is without any basis, only to return in the zval structure (C's built-in data structure used to store PHP variables) stored in the known length of the string. However, because strlen () is a function, it is still somewhat slow because the function call requires several steps, such as lowercase (Yi Zhu: refers to the function name and lowercase, PHP does not distinguish between function names case-sensitive), hash lookup followed by the implementation of said function. In some cases, you can use isset () techniques to accelerate the implementation of your code.

(Example below)
if (strlen ($ foo) <5) (echo "Foo is too short" $ $)
(Compare with the following skills)
if (! isset ($ foo (5))) (echo "Foo is too short" $ $)

Call to isset () happens than strlen () speed, because unlike the latter, isset () as a language structure, meaning that its implementation does not require function lookups and lowercase. That is, in fact, the string length in the test code on top of you did not spend too much overhead.

34, when the variable $ i to increase or decrease the time, $ i + + than + + $ i slower. This difference is a PHP specific and does not apply to other languages, so please do not change your C or Java code thinking it'll suddenly become faster, it will not. + + $ I is faster because it only requires 3 instructions (opcodes), $ i + + requires 4 instructions. Post incrementation actually causes a temporary variable, this temporary variable is then incremented. The pre-increment increasing the original value directly. This is the most optimized one, as Zend's PHP optimizer has done. Remember that this optimization would be a good idea, because not all of the instructions optimizer will do the same optimization, and there are plenty without an opcode optimizer Internet service providers (ISPs) and the server.

35, not everything has to be object oriented (OOP), object-oriented are often much overhead, each method and object call consumes a lot of memory.

36, Do not implement every data structure, the array is also useful.

37, Do not split methods too much, think about what you really intend to reuse, which code?

38, when you need, you can always split the code of a method.

39, as far as possible a large number of PHP built-in functions.

40, if the code exists in time consuming functions, you may consider using C extension means to achieve them.

41, assessment test (profile) your code. Checker will tell you which parts of the code consumes how many time. Xdebug debugger already contains a test program to assess the overall test can show you the bottlenecks.

42, mod_zip as Apache module compresses your data, and allows data transmissions are reduced 80%.

43, can use file_get_contents in alternative file, fopen, feof, fgets etc. methods as far as using file_get_contents, because he was much more efficient! But note file_get_contents to open a URL in the PHP version of the file when the problem;

44, as the few to file operations, although the efficiency of PHP file operations are not low;

45, optimized Select SQL statements, where possible, to minimize the conduct of Insert, Update operation (in the update, I was bad batch of them);

46, possible to use PHP internal functions (but I have to find a PHP function which does not exist, waste could write a custom function of time, experience issue, ah!);

47, do not declare variables inside the loop, especially the large variable: object (which is PHP which does not seem to pay attention to the problem, right?);

48, try not to cycle nested multidimensional array assignment;

49, can be used in the PHP string manipulation functions within the case, do not use regular expressions;

50, foreach efficient as possible and for using foreach instead of while loop;

51, replace double quotes with single quotes quoted strings;

52, "with i + = 1 instead of i = i +1. Consistent with c / c + + practice, efficiency is also high";

53, on the global variables should be used to unset () out;

Declined comment