PHP code optimization Tips for a large inventory

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

The purpose of PHP optimization is to spend the least amount of time on the fastest running speed and the easiest to maintain code. This article gives you a comprehensive optimization technique.

1. Echo is faster than print.

2. Use Echo's multiple parameters 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 cycle, preferably using foreach instead.

4, the global variable, should be used up on 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 increment or decrement of the variable $i is executed, $i + + will be slower $i than + +. 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, the use of Var_dump PHP code debugging. 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 all times, 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. Blocking error messages with @ is very inefficient.

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

16. Incrementing an undefined local variable is 9 to 10 times times slower than incrementing a pre-defined local variable.

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)

19, Apache parsing a PHP script time is 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 very expensive, each method and object calls will consume 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.

PHP code optimization Tips for a large inventory

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.