Summary of tips for optimizing PHP code
- 2020-06-03 06:08:03
Summary of tips for optimizing PHP code
1. If a method can be static, declare it static and speed increases by 1/4;
2. echo is more efficient than print because echo does not return a value and print returns an integer.
3. Set the maximum number of cycles before the cycles, but not during the cycles;
4. Destroy variables to free memory, especially large arrays;
___, SUCCset, SUCCautoload and other magic methods should be avoided.
6. requiere_once() is relatively resource-intensive;
7. Use absolute paths in includes and requires to spend less time analyzing paths;
8. If you need sexinsex to script execution time, $_SERVER['REQUSET_TIME'] is better than time();
9. If you can use character processing functions, use them as much as possible, because the efficiency is higher than regular; //
10. str_replace is faster than regular preg_replace, but strtr is 1/4 faster than str_replace.
11. If a function can take both an array and a simple character as an argument, such as character substitution, and the argument list
It's not too long. Consider using one more concise substitution statement, replacing only one character at a time, rather than accepting an array
As a find and replace parameter. Minimize the impact. 1+1 > 2;
12. Using @ to cover up errors will slow down the running speed of scripts;
13. $row['id'] is 7 times faster than $row[id].
14. Error messages are useful;
15. Don't use functions in loops, such as For($x=0; $x < count ($array); $x), the count() function is calculated outside;
16. It is two times slower to create a global variable than a local variable;
17. Create an object property (class variable) such as ($this-) > prop++) is three times slower than local variables;
18. Creating an undeclared local variable is 9-10 times slower than creating an initialized local variable;
19. Specifying a global variable that is not used by any one function can also degrade performance (and declare the same number of local changes
PHP may check to see if the global variable exists.
20. Method performance does not depend on the number of methods defined in a class, because I add 10 or more methods
There is no difference in the performance of the methods in the test class (before and after the test method);
21. The performance of methods in subclasses is better than that in base classes;
22. A function that calls only one argument and has an empty body takes 7-8 operations of $localvar++, and
A similar method (a function in a class) runs approximately 15 $localvar++ operations;
23. It is faster to use commas instead of dots when outputting strings. Note: this only works for echo. This function can connect
Takes 1 string as argument;
24. An php script page on apache server costs at least 2-10 times more than the corresponding HTML static page generation
It is recommended to use more static HTML pages and a small amount of steps.
Unless you have a cache installed, your php scripts will need to be recompiled every time they are accessed. It is recommended to install an php
Caching programs that obviously improve your performance by 20-100% by removing 1 of the duplicate compilations;
26. memcached, a high-performance distributed memory object caching system, is recommended to improve the performance of dynamic network applications.
Reducing the burden of databases;
27. Use the ip2long() and long2ip() functions to turn IP addresses into integers and store them in the database instead of characters. It's almost healthy
1/4 lower storage space. At the same time, it is easy to sort the address and find it quickly.
28. Use checkdnsrr() to verify the validity of some email addresses by domain name existence. This built-in function guarantees this
Each domain name corresponds to 1 IP address;
29. If you are using versions of php5 and above, consider using the improved mysql_* function mysqli_*.
30. Try to like using the 3 - element operator (? :);
31. Before you think about completely reworking your project, see if PEAR has anything you need. PEAR is a huge resource,
Many php developers know this;
32. highlight_file() can automatically print a copy of the source code of a well-formatted page;
33. 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 php.ini file. However, if you are using a Shared virtual host, php.ini, you cannot change it.
Then you'd better add the error_reporting(0) function to the first line of each script file (or use
require_once() to load) this effectively protects sensitive SQL queries and paths from being displayed in case of error;
34. Use gzcompress() and gzuncompress() to compress (uncompress) the in (out) number of large strings
According to the library. This built-in function can be compressed to 90% using gzip algorithm;
35. Multiple return values for a function by reference to the address of the parameter variable. You can put a variable in front of it. & "To say
By address rather than by value;
36. Using strlen() is not very fast because it calls 1 other operations such as lowercase and hash table queries,
A similar function can be achieved with isset(), which is faster than strlen();