PHP Coding Skills
Use single quotes instead of double quotes to contain the string, this will be faster. Because PHP looks for variables in a string enclosed in double quotes, single quotes are not, note: only works with echo, which is a way to put several strings as arguments of "function" (translation: PHP Manual He said echo is the structure of language, not a real function, so the function with the double quotes).1. if the class method can be defined as static, as far as possible be defined as static, its speed will increase nearly four-fold.
2. the speed $ row ['id'] is $ row [id] seven times.
3. echo faster than print, and the echo of using multiple parameters: instead of string concatenation (translation refers to a comma instead of a period), for Example: echo $ str1, $ str2.
4. before executing the for loop to determine the maximum number of cycles, not in the loop maxvalue best use foreach instead.
5. Unset your variables, especially large arrays in order to free up memory.
6. try to avoid using __get, __ set, __ autoload.
7. require_once () is expensive.
8. try to use absolute paths include file, because it avoids the PHP include_path to find the file in the speed, the time required to resolve the OS paths less.
9. if you want to know the script started executing (translation: the server receives a client request) time, using $ _SERVER ['REQUEST_TIME'] better than time ().
10. instead of the regular expression functions perform the same function.
11. str_replace function faster than preg_replace, but strtr is four times the str_replace function.
12. if a string replacement function, accepts both arrays and single characters as arguments, and the parameter length is not too long, consider writing a few redundant replacement code, so that each pass parameter is a character, rather than just write a line of code that accepts arrays as search and replace arguments.
13. using the select statements (translation: the switch case) is better than using a plurality of if, else if statement.
14. @ shielded by the error message is very inefficient practice, extremely inefficient.
15. open the apache mod_deflate module can improve web browsing speed.
16. when the database connection should be switched off after use, do not use long connection.
17. Error messages are expensive.
18. incrementing a local variable in a method is the fastest. Almost calling a local variable in a function fairly.
19. incrementing a global variable slower than a local variable twice.
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 is 9-10 times slower.
22. Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists.
23. it seems the number of method calls methods defined in the class has nothing to do, because I (before and after the test method) added 10 method, but no change in performance.
24. the derived class method up and running faster in the same manner as defined in the base class.
25. empty function call with one parameter, the time it takes to perform the equivalent of a local variable 7-8 times the increment operation. A similar method call time spent close to 15 times the local variable increment operation.
26. Apache A PHP script than a static HTML page 2-10 times slower. Try to use more static HTML pages and fewer scripts.
27. unless the script can be cached, or when each call will be recompiled. Install a PHP caching product to typically increase 25-100% of the performance, by removing compile.
28. try to do the cache, you can use memcached. memcached is a high-performance memory object caching system intended to speed up dynamic Web applications by alleviating database load. The op code (OP code) caches are useful so that your script does not have to be compiled for each request.
29. when the operator needs to verify the length of the string and meets certain requirements, you assume will use strlen () function. This function is pretty quick, because it does not do any calculation, only to return a known length of the string stored in zval structure (C built-in data structures used to store variables in PHP). However, because strlen () is a function it is still somewhat slow because the function call requires several steps, such as lowercase letters (translation: that of the function name in lowercase, PHP function names are not case-sensitive), hash lookup, will follow the function is called to perform together. In some cases, you can use the isset () trick to accelerate the execution of your code.
Example below
if (strlen ($ foo) <5) {echo "Foo is too short";}
(Compare with the following tips)
if {echo "Foo is too short";} (isset ($ foo {5})!)
Calling isset () happens than strlen () fast, because unlike the latter is, isset () is a language construct, meaning that it's execution does not require function lookups and lowercase technology. That is, in fact, the top-level test string length code you do not have to spend too much overhead.
34, when the value of the variable $ i is incremented or decremented, $ i ++ will be slower than i ++ $ number. This is something PHP specific and does not apply to other languages, so do not go modifying your C or Java code thinking it'll suddenly become faster, it will not. ++ $ I is faster because it requires three instructions (opcodes), $ i ++ you need four instructions. Postincrement actually produce a temporary variable, this temporary variable is then incremented. While pre - incrementation increases the original value directly. This is one of the optimization process, as Zend's PHP optimizer done in the case. Keep in mind this optimization process is a good idea, since not all opcode optimizers do the same optimization process, and there are a lot of no assembly instructions optimizing Internet service providers (ISPs) and servers.
35. not everything has to be object oriented (OOP), object-oriented often much overhead, each method and object call consumes a lot of memory.
36. Do not implement all of the data structures, arrays are also useful.
37. Do not split methods too much, I think you really intend to reuse what code?
38. when you need to, you can always code into methods.
39. try to use a lot of PHP built-in functions.
40. if a large number of time consuming functions in your code, you can consider them as C extensions.
41. assessment test (profile) your code. Checker will tell you which parts of your code consumes much time. Xdebug debugger already contains a test program to assess the possible test shows you the bottlenecks in general.
42. mod_zip available as an Apache module compresses your data and allows data transmission was reduced by 80%.
43. you can use file_get_contents in alternative file, the case fopen, feof, fgets other methods, try to use file_get_contents, because his efficiency is much higher! But beware PHP version file_get_contents problems when opening a URL file;
44. try less operation documents, although the efficiency of PHP file operations is not low;
45. optimization of SELECT SQL statements, where possible, to minimize the situation INSERT, UPDATE operations (in the update, I have been granted over evil);
46. as far as possible the use of PHP internal functions (but I have to find a function which does not exist in PHP, waste could write a custom function of time, experience, problem ah!);
47. do not declare variables inside the loop, especially the large variables: Object (? It just does not seem to pay attention to the problem of PHP inside it);
48. try not to loop nest multidimensional array assignment;
49. in the interior can be the case with PHP string manipulation functions, do not use regular expressions;
50. foreach higher efficiency, try to replace the while and for foreach loop;
51. with single quotes replace double-quoted strings;
52. "with i + = 1 instead of i = i + 1 in line with c / c ++ habits, efficiency is still high.";
53, for global variables should run out unset () away.
Post A Comment:
0 comments: