PHP Code Optimization Tips Summary, PHP tips Summary _php Tutorial

Source: Internet
Author: User
Tags switch case how to use sql

PHP Code Optimization Tips Summary, PHP tips Summary

The purpose of PHP optimization is to spend the least amount of time on the fastest running speed and the easiest to maintain code. Here are some optimization tips for you:

1. Echo is faster than print.

2. Use Echo's multiple arguments instead of string connections.

3. To determine the maximum number of cycles before executing a for loop, do not calculate the maximum value once per loop, preferably using foreach instead.

4. For global variables, it should be unset () off.

5. It is quicker to use single quotes instead of double quotes to contain strings. Because PHP searches for a variable in a string surrounded by double quotes, the single quotation mark does not.

6. function instead of regular expression to complete the same function.

7. When the variable $i is incremented or decremented, $i + + will be slower $i than the + +. This difference is unique to PHP, not for other languages, + + $i faster because it requires only 3 instructions (opcodes), $i + + requires 4 instructions. The post increment actually produces a temporary variable, which is then incremented. The predecessor increment is incremented directly on the original value.

8. Using the Select Branch statement (switch case) is better than using multiple if,else if statements.

9. Use Var_dump to debug PHP code. If you are looking for PHP debugging techniques, I have to say that var_dump should be the target you are looking for, and that this command will meet all your needs in terms of displaying PHP information, and most of the debugging code is related to getting the values in PHP.

10. The full path is used when the file is included, and less time is required to resolve the operating system path.

11. It's a bad idea to create global values at will, but sometimes it does. Using global values for database tables or database connection information is a good idea, but do not use global values frequently in your PHP code. In addition, a better approach is to put your global variables in a config.php file.

12. If you want to know when the script starts executing, use $_server[' request_time '] better than time ().

13. Open the Apache mod_deflate module.

14. Masking error messages with @ is very inefficient.

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

16. Incrementing a non-predefined local variable is 9 to 10 times times slower than incrementing one of the pre-defined local variables.

17. A method in a derived class runs faster than the same method defined in the base class.

18. Defining only a local variable without calling it in the function also slows down the speed (which is equivalent to incrementing a local variable)

Apache parses a PHP script 2 to 10 times times slower than parsing a static HTML page. Use static HTML pages as much as possible and use fewer scripts.

20. As mentioned earlier, 99% of the most important parts of any PHP Web site may be databases. Therefore, you need to be very familiar with how to use SQL correctly, learn related tables and more advanced database technologies.

21. Call an empty function with one parameter, which takes the same amount of time as performing a local variable increment operation of 7 to 8 times.

22. When you manipulate a string and need to verify that its length satisfies a certain requirement, you will of course use the strlen () function. This function executes quite quickly because it does not do any calculations and only returns the length of the known string stored in the Zval structure (the built-in data structure of C, used to store PHP variables).

23. Not all cases must use object-oriented development, object-oriented tends to be expensive, and each method and object invocation consumes a lot of memory.

24. Unless the script can be cached, it will be recompiled every time it is called. Introducing a set of PHP caching mechanisms can typically improve performance by 25% to 100% to eliminate compilation overhead.

Finally, let's remind you of the things you should be aware of when optimizing your code:

1. Short code is not equal to fast code
Many people want to write a program as succinctly as possible, but shorter code sometimes takes longer to execute.

2. When writing a program, it is more important to focus on the extensibility of the program than on the speed of the pursuit.

3. Before optimizing your code, take a look at the database-related sections, because most application bottlenecks are in the database rather than the code.

4. Micro-optimization not worth the candle
What is micro-optimization? As mentioned earlier, the code for the regular Expression section is replaced with a string function instead. This has the following drawbacks:
(1) Take a long time
(2) Does not fundamentally address performance issues
(3) It is very likely that the previous code will be destroyed to produce an unknown error
(4) Pay more than return

There is also a misconception that some people, in order to make the program more optimized, consider the optimization in the analysis of the business logic, in order to get better code and change the business logic. This is a very foolish idea, because the purpose of the procedure is to deal with the problems encountered in reality and we cannot get the cart before the horse.

The above is the PHP code optimization method and some tips, hoping to help you better optimize PHP code true techarticle Summary of PHP code optimization tips, PHP Tips Summary The purpose of PHP optimization is to spend the least cost in exchange for the fastest running speed and the easiest to maintain the code. Here are some optimization tips for you: ...

  • 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: 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.