40 small instances optimized for php code

Source: Internet
Author: User
If a method can be static, it is declared as static, and the speed can be improved by 1/4. echo is more efficient than print. because echo does not return a value, print returns an integer; set the maximum number of cycles before the loop, not in the loop. 1. if a method can be static, declare it 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 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 the efficiency is higher than that of regular expressions;

10. the replacement of str_replace is faster than the replacement of regular expressions with preg_replace, but the replacement of strtr is 1/4 faster than that of str_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 @ reduces 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. For example, For ($ x = 0; $ x <count ($ array); $ x) and count () functions are calculated first;

16. creating local variables in a method is the fastest, 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 reduces the performance (the same as declaring the same number of local variables). PHP may check whether the global variable exists;

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.