40 tips for PHP code flying (improving php efficiency) 1. if a method can be static, it is declared as static, and the speed can be increased by 1/4; 2. echo is more efficient than print. because echo does not return a value, print returns an integer. 3. set the maximum number of loops before the loop, instead of in the loop; 4. 40 tips for PHP code to fly (improving php efficiency)
1. if a method can be static, declare it as static, and the speed can be increased by 1/4;
2. the echo efficiency is higher than that of print. because echo does not return a value, print returns an integer;
3. set the maximum number of cycles before the loop, not in the loop;
4. destroy variables to release memory, especially large arrays;
5. avoid using magic methods such as _ get, _ set, and _ autoload;
6. requiere_once () is resource-consuming;
7. use absolute paths in pair Des and requires, so that the analysis path takes less time;
8. if you need sexinsex to the TIME when the script is executed, $ _ SERVER ['requset _ time'] is better than time ();
9. if you can use character processing functions, try to use them because they are more efficient than regular expressions;
10. replace str_replace is faster than replace preg_replace with regular expressions, but strtr is 1/4 faster than replace;
11. if a function can accept both arrays and simple characters as parameters, such as character replacement, and the parameter list is not too long, you can use some simple replacement statements, replace only one character at a time, instead of accepting arrays as search and replacement parameters. Minor events, 1 + 1> 2;
12. masking errors with @ will reduce the script running speed;
13. $ row ['id'] is 7 times faster than $ row [id]. we recommend that you use array keys and quotation marks;
14. the error message is useful;
15. do not use functions in a loop, such as For ($ x = 0; $ x <count ($ array); $ x) and count () functions are calculated first;
16. setting up local variables in a method is the fastest, and 97xxoo is almost as fast as calling local variables in a method;
17. creating a global variable is twice slower than a local variable;
18. create an object property (the variables in the class). For example, ($ this-> prop ++) is three times slower than the local variable;
19. Creating an undeclared local variable is 9-10 times slower than an initialized local variable;
20. declaring a global variable that has not been used by any function also reduces the performance (the same as declaring the same number of local variables). PHP may check whether the global variable exists;
21. the performance of a method is irrelevant to the number of methods defined in a class, because I add 10 or more methods to the test class (these methods are before and after the test method) there is no difference in post-performance;
22. the method in the subclass has better performance than in the base class;
23. it takes 7-8 times to run a function that calls only one parameter and the function body is empty $ localvar ++, and a similar method (function in the class) run the $ localvar ++ operation about 15 times;
24. Surrounding your string by 'instead of "will make things interpret a little faster since php looks for variables inside"…" But not inside '... '. Of course you can only do this when you don't need to have variables in the string.
25. use commas (,) to replace vertices in the output string for faster segmentation. Note: this only applies to echo. This function can take some strings as parameters;
26. a php script page on the apache server takes at least 2-10 times more time than the corresponding HTML static page generation. we recommend that you use static HTML pages and a small amount of steps;
27. unless you have installed the cache, your php script needs to be recompiled every time it is accessed. We recommend that you install a php Cache program to significantly improve the performance of 20-100% by removing repeated compilation;
28. we recommend using memcached, a high-performance distributed memory object cache system, to improve the performance of dynamic network applications and reduce the burden on databases;
29. use the ip2long () and long2ip () functions to convert the IP address to an integer type and store it in the database instead of the primary type. This can reduce storage space by almost 1/4. At the same time, it is easy to sort and quickly find the addresses;
30. use checkdnsrr () to confirm the validity of some email addresses through the existence of domain names. This built-in function can ensure that each domain name corresponds to an IP address;
31. if you are using php5 and mysql4.1 or later versions, consider using the mysql _ * improved function mysqli _*;
32. try to use the ternary operator (? :);
33. before you want to completely redo your project, check if PEAR has what you need. PEAR is a huge resource library that many php developers know;
34. use highlight_file () to automatically print a copy of the well-formatted page source code;
35. use the error_reporting (0) function to prevent potential sensitive information from being displayed to users. Ideal error reports should be completely disabled in the php. ini file. However, if you are using a shared virtual host. ini you cannot modify, so you 'd better add the error_reporting (0) function and put it in the first line of each script file (or load it using require_once) this effectively protects sensitive SQL queries and paths from being displayed when an error occurs;
36. use gzcompress () and gzuncompress () to compress () large-capacity strings (decompress) and store them in (retrieve) the database. This built-in function can be compressed to 90% using the gzip algorithm;
37. make a function have multiple return values by referencing the parameter variable address. You can add "&" before the variable to indicate passing by address rather than by value;
38. fully understand "magic quotes" and the dangers of SQL injection. i'm hoping that most developers reading this are already familiar with SQL injection. however, I list it here because it's absolutely critical to understand. if you 've never heard the term before, spend the entire rest of the day googling and reading.
39. strlen () is used because it is not very fast to call other operations such as lowercase and hash table queries. we can use isset () to implement similar functions. isset () faster than strlen ();
40. when incrementing or decrementing the value of the variable $ I ++ happens to be a tad slower then ++ $ I. this is something PHP specific and does not apply to other versions, so don't go modifying your C or Java code thinking it'll suddenly become faster, it won't. ++ $ I happens to be faster in PHP because instead of 4 opcodes used for $ I ++ you only need 3. post incrementation actually causes in the creation of a temporary var that is then incremented. while pre-incrementation increases the original value directly. this is one of the optimization that opcode optimized like Zend's PHP optimizer. it is a still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer.
Detailed source reference: http://www.jb51.net/article/23019.htm