PHP code optimization set
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 the absolute path in pair des and requires
8. If you need to get the script execution TIME, $ _ 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. The fastest speed for creating local variables in a method is almost as fast as calling local variables in a method;
OfAdmin. Com
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;
OfAdmin. Com
26. On the apache server, a php script page 2nxx 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 number 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; ofAdmin. com
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.