PHP code optimization tips and php tips _ PHP tutorials

Source: Internet
Author: User
Tags switch case how to use sql
PHP code optimization tips and php tips. PHP code optimization tips summary, php tips summary PHP optimization aims to get the fastest running speed and the most easily maintained code at the lowest cost. The following optimization tips are provided: PHP code optimization tips and php tips

The goal of PHP optimization is to get the fastest running speed and the most easily maintained code at the lowest cost. The following optimization techniques are provided:

1. echo is faster than print.

2. replace string connection with multiple echo parameters.

3. determine the maximum number of cycles before executing the for loop. do not calculate the maximum value for each loop. use foreach instead.

4. unset () should be used up for global variables.

5. replace double quotation marks with single quotes to include strings, which is faster. Because PHP searches for variables in strings enclosed by double quotation marks, but not in single quotes.

6. functions use the same functions instead of regular expressions.

7. when the execution variable $ I increments or decreases, $ I ++ is slower than ++ $ I. This difference is exclusive to PHP and does not apply to other languages. ++ $ I is faster because it only requires three commands (opcodes) and $ I ++ requires four commands. In fact, a temporary variable is generated in post-increment mode, which is then incremented. The pre-increment directly increases on the original value.

8. switch case is better than if and else if statements.

9. use var_dump to Debug PHP code. If you are looking for php debugging technology, I must say that var_dump should be your target. this command can meet all your needs in displaying php information, most of the debugging code is related to obtaining the value in PHP.

10. if you use the full path when the file is included, it takes less time to parse the operating system path.

11. it is a bad practice to easily create a global value, but sometimes the actual situation does need to be done again. It is a good idea to use global values for database tables or database connection information, but do not use global values frequently in your PHP code. In addition, a better way is to store your global variables in a config. php file.

12. if you want to know the TIME when the script starts to run, use $ _ SERVER ['request _ time'] instead of time ().

13. open the mod_deflate module of apache.

14. blocking error messages with @ is very inefficient.

15. try to use a large number of PHP built-in functions.

16. increasing an unspecified local variable is 9 to 10 times slower than increasing a predefined local variable.

17. the methods in the derived class run faster than the same methods defined in the base class.

18. defining only one local variable without calling it in the function also slows down (to the extent that it is equivalent to increasing one local variable)

19. Apache parses a PHP script two to ten times slower than parsing a static HTML page. Use static HTML pages and less scripts as much as possible.

20. as mentioned before, 99% of the most important parts of any php website may be databases. Therefore, you need to be familiar with how to use SQL correctly and learn to associate tables and more advanced database technologies.

21. calling an empty function with a parameter takes seven to eight times to increment local variables.

22. when you operate a string and check whether its length meets certain requirements, you will use the strlen () function. This function is executed quite quickly because it does not perform any calculations and only returns the known string length stored in the zval structure (the built-in data structure of C, used to store PHP variables.

23. not all situations require the use of object-oriented development. object-oriented usually has a high overhead and each method and object call consumes a lot of memory.

24. unless the script can be cached, it will be re-compiled every time it is called. The introduction of a PHP Cache mechanism can generally improve the performance by 25% to 100%, so as to avoid compilation overhead.

Finally, I would like to remind you of the things you should pay attention to during code optimization:

1. short code is not equal to quick code
Many people want to write the code more concisely and better, but the shorter the code, the longer the execution time.

2. when writing a program, you should pay more attention to program scalability rather than speed.

3. before optimizing your code, first look at the database-related part, because the bottleneck of most applications lies in the database rather than the code.

4. micro-optimization is not worth the candle
What is microoptimization? As mentioned above, the code in the regular expression part is replaced by a string function. This method has the following disadvantages:
(1) It takes a long time
(2) won't fundamentally solve performance problems
(3) it is very likely that the previous code will be damaged to generate unknown errors.
(4) pay is greater than return

I have to mention a misunderstanding here. some people take optimization into consideration when analyzing the business logic to make the program more optimized, so as to change the business logic for better code. This is a very stupid idea, because the purpose of the program is to deal with problems encountered in reality, and we cannot put the cart before the horse.

The above are the php code optimization methods and tips, hoping to help you better optimize php code.

The goal of optimize the PHP runtime is to get the fastest running speed and the most easily maintained code at the lowest cost. The following optimization techniques are provided :...

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.