PHP code optimization Excerpt <1>

Source: Internet
Author: User
PHP code optimization excerpt & lt; 1 & gt; 1. if a method can be static, it will be declared statically, and the speed will be increased to 4 times. 2. echo is faster than print, and multiple echo parameters are used to replace string connection. 3. determine the maximum number of cycles before executing the for loop. do not calculate the maximum value for each loop. For ($ I = 0; $ I & lt; co PHP code optimization excerpt <一>

1. if a method can be static, it will be declared statically, and the speed can be increased to 4 times.
2. echo is faster than print, and multiple echo parameters are used to replace string connection.
3. determine the maximum number of cycles before executing the for loop. do not calculate the maximum value for each loop. For ($ I = 0; $ I 4. unregister unnecessary variables, especially large arrays, to release the memory.
5. avoid using _ get _ set _ autoload whenever possible .?
6. require_once () is expensive.
7. if you use the full path when the file is included, it takes less time to parse the operating system path.
8. if you want to know the TIME when the script starts to run, $ _ SERVER ['request _ time'] is better than time ()
9. functions use the same functions instead of regular expressions.
10. strtr functions are four times more efficient than str_replace functions.
11. it is better to use multiple if, elseif
12. blocking error messages with @ is very inefficient.
13. when the database connection is completed, close it in time.
14. $ val ['id'] is much more efficient than $ val [id.
15. increasing a local variable is much slower than increasing a local variable. increasing an object attribute ($ this-> prop ++) is much slower than increasing a local variable.
16. defining only one local variable 2 will also slow down if it is not called in the function. A method call is independent of the number of methods defined in the class.
17. the methods in the derived class run faster than the same methods defined in the base class.
18. the efficiency of using single quotes is faster than that of double quotes, because PHP searches for variables in strings surrounded by double quotes.
19. Apache parses a PHP script twice to 10 times slower than parsing an HTML page, and uses as many static HTML scripts as possible.

Related Article

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.