Tips for optimizing PHP code
1. If a method can be static, then declare that he is static, the speed can be increased by 1/4;
2. Echo is more efficient than print, because Echo has no return value, and print returns an integer type;
3. Set the maximum number of cycles before the loop, not in the loop;
4. Destroy the variable to release the memory, especially the large array;
5. Avoid using magic methods like __get, __set, __autoload, etc.;
6. Requiere_once () comparative consumption of resources;
7. Use absolute paths in includes and requires, so that less time is spent on the analysis path;
8. If you need to sexinsex to the time of the script execution, $_server[' requset_time ' is better than times ();
9. If you can use character processing functions, use them as much as possible, because efficiency is higher than regular;//
Str_replace character replacement singular is replaced preg_replace fast, but STRTR is 1/4 faster than str_replace;
11. If a function can accept both an array and a simple character as a parameter, such as a character substitution, and the argument list
Not too long, consider using more concise substitution statements, replacing only one character at a time, rather than accepting an array
As a find and replace parameter. Punches,1+1>2;
12. Masking errors with @ will slow down the script;
$row [' ID '] is 7 times times faster than $row[id], it is recommended to form the habit of array key quotes;
14. Error messages are useful;
15. Do not use functions in the loop, such as for ($x =0; $x < count ($array); $x), the count () function is calculated first outside;
16. Creating a global variable is twice times slower than local variables;
17. Create an object property (the variable inside the class) for example ($this->prop++) is 3 times times slower than local variables;
18. Creating an undeclared local variable is 9-10 times slower than an initialized local variable;
19. A global variable that is not used by any function also degrades performance (and declares the same number of local
, PHP may check to see if the global variable exists;
20. The performance of the method is not related to the number of methods defined within a class, because I add 10 or more methods
There is no difference in performance after the test class (these methods are before and after the test method);
21. The performance of the method in the subclass is better than that in the base class;
22. Only one parameter is called and the function that the function body is empty is equal to 7-8 times the $localvar++ operation, and
A similar method (a function in a class) runs equal to approximately 15 $localvar++ operations;
23. Using commas instead of point segmentation is faster when outputting strings. Note: This only works for Echo, and this function can be connected
Subject to some strings as arguments;
24. A PHP script page in the Apache server will take at least 2-10 times more than the corresponding HTML static page generation
Time, it is recommended to use more static HTML pages and a small number of footsteps;
25. Unless your cache is installed, your PHP script will need to be recompiled each time it is accessed. Recommended installation of PHP
Caching programs so that you can obviously improve the performance of your 20-100% by removing some repetitive compilations;
26. It is recommended to use memcached, high-performance distributed memory object cache system to improve dynamic network application performance,
Reduce the burden on the database;
27. Use the Ip2long () and LONG2IP () functions to convert the IP address into an integer type into the database instead of the character type. This can almost drop
1/4 less storage space. At the same time, the address can be easily sorted and quickly find;
28. Use CHECKDNSRR () to confirm the validity of some email addresses through the existence of the domain name, this built-in function can guarantee
Each domain name corresponds to an IP address;
29. If you are using PHP5 and mysql4.1 versions above, consider using the mysql_* modified function mysqli_*;
30. Try to prefer to use the ternary operator (?:);
31. Before you want to thoroughly re-do your project, see if PEAR has anything you need. PEAR is a huge repository of resources,
Many PHP developers know;
32. Use Highlight_file () to automatically print a well-formatted copy of the page source code;
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 the php.ini file. But if you are using a shared virtual host, php.ini you cannot modify,
Then you'd better add the error_reporting (0) function and put it in the first line of each script file (or
Require_once () to load) This can effectively protect sensitive SQL queries and paths from being displayed when errors occur;
34. Use Gzcompress () and gzuncompress () to compress (extract) the number of large-size strings in (out)
When it comes to libraries. This built-in function can be compressed to 90% using the GZIP algorithm;
35. Make a function have multiple return values by referring to the argument variable address. You can put a "&" in front of the variable to indicate
Pass by address rather than by value;
36. Use strlen () because you want to invoke some other operations such as lowercase and hash table queries so the speed is not too good,
We can use Isset () to achieve similar functions, isset () speed is better than strlen ();
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service