Php code optimization skills

Source: Internet
Author: User
PHP code optimization tips 1. if a method can be static, it is declared as static, and the speed can be improved by 14; 2. echo is more efficient than 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; optimize PHP code skills
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. use character processing functions as much as possible because the efficiency is higher 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
It is not too long. you can use some simple replacement statements to replace only one character at a time, instead of accepting arrays.
As a search and Replacement parameter. 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. creating a global variable is twice slower than a local variable;
17. create an object property (the variables in the class). For example, ($ this-> prop ++) is three times slower than the local variable;
18. Creating an undeclared local variable is 9-10 times slower than an initialized local variable;
19. a global variable that has not been used by any function will also reduce the performance (and declare the same number of local variables
), PHP may check whether the global variable exists;
20. The performance of a method is irrelevant to the number of methods defined in a class, because I add 10 or more methods.
There is no performance difference between the tested classes (before and after the testing methods;
21. the method in the subclass has better performance than in the base class;
22. it takes 7-8 times to run a function that calls only one parameter and the function body is empty.
A similar method (a function in the class) is executed for about 15 times $ localvar ++;
23. when the output string is used, use a comma instead of a dot to separate it more quickly. Note: this only applies to echo. This function can be connected
Take some strings as parameters;
24. on the apache server, a php script page is at least two to ten times more than the corresponding HTML static page generation.
Time, it is recommended to use more static HTML pages and a small number of steps;
25. unless you have installed the cache, your php script needs to be recompiled every time it is accessed. We recommend that you install php
Cache program, which significantly improves your performance by 20-100% by removing repeated compilation;
26. we recommend using memcached, a high-performance distributed memory object cache system, to improve the performance of dynamic network applications,
Reduce the burden on the database;
27. 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 almost reduces
1/4 lower storage space. At the same time, it is easy to sort and quickly find the addresses;
28. use checkdnsrr () to confirm the validity of some email addresses through domain name existence. This built-in function can ensure
Each domain name corresponds to an IP address;
29. if you are using php5 and mysql4.1 or later versions, consider using the mysql _ * improved function mysqli _*;
30. try to use the ternary operator (? :);
31. before you want to completely redo your project, check if PEAR has what you need. PEAR is a huge resource library,
Many php developers know that;
32. use highlight_file () to automatically print a copy of the well-formatted page source code;
33. use the error_reporting (0) function to prevent potential sensitive information from being displayed to users. The ideal error report should be
Completely disabled in the php. ini file. However, if you are using a shared virtual host, you cannot modify php. ini,
Then you 'd better add the error_reporting (0) function and place it in the first line of each script file (or use
Require_once () to load) this effectively protects sensitive SQL queries and paths from being displayed when errors occur;
34. use gzcompress () and gzuncompress () to compress () large-capacity strings (decompress) and store (extract) the strings.
Database. This built-in function can be compressed to 90% using the gzip algorithm;
35. make a function have multiple return values by referencing the parameter variable address. You can add "&" before the variable to indicate
Transfer by address rather than by value;
36. using strlen () is not very fast because it needs to call some other operations such as lowercase and hash table queries,
We can use isset () to implement similar functions. isset () is faster than strlen ();

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.