PHP Code Optimization techniques Summary _php Skills

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

The goal of PHP optimization is to get the fastest running speed and the easiest code to maintain with the least cost. To provide you with the following optimization techniques:

1. Echo is faster than print.

2. Use Echo's multiple parameters instead of string concatenation.

3. Determine the maximum number of loops before executing the For loop, and do not compute the maximum for Each loop, preferably using foreach instead.

4. For global variables, you should use them unset () out.

5. It is quicker to use single quotes instead of double quotes to contain strings. Because PHP searches for variables in a string enclosed in double quotes, single quotes do not.

6. Functions do the same function instead of regular expressions.

7. When the execution variable $i is incremented or decremented, $i + + $i slower than the + +. This difference is specific to PHP and does not apply to other languages, + + $i faster because it requires only 3 instructions (opcodes), $i + + requires 4 instructions. A post increment actually produces a temporary variable, which is then incremented. And the predecessor increment increases 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're looking for PHP debugging technology, I have to say that var_dump should be the target you're looking for, and this command can 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. Using the full path when containing the files will require less time to resolve the operating system path.

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

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

13. Open the Apache mod_deflate module.

14. Using the @ Block error message 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 predefined local variable.

17. Methods in a derived class run faster than the same method defined in the base class.

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

apache resolves 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 less scripting.

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 relational tables, and more advanced database technologies.

21. Call an empty function with one parameter, which takes as much time as 7 to 8 local variable increment operations.

22. When manipulating strings and having to test whether their lengths meet certain requirements, you will of course use the strlen () function. This function executes fairly quickly because it does not do any calculations and returns only the known string lengths stored in the Zval structure (c's built-in data structure for storing PHP variables).

23. Not all cases must use object-oriented development, object-oriented is often expensive, 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 invoked. The introduction of a set of PHP caching mechanisms typically increases performance from 25% to 100% to exempt compilation overhead.

Finally, remind yourself 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 the code as concise as possible while writing the program, but the shorter code sometimes takes longer to execute.

2. When writing a program, you should pay more attention to the expansion of the program, rather than the pursuit of speed.

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

4. Micro-optimization outweigh the gains
What is called micro-optimization? Replace the code in the regular Expression section with a String function, as mentioned earlier. This has the following disadvantages:
(1) Take a long time
(2) Does not fundamentally solve performance problems
(3) It is likely that the previous code will be corrupted to produce an unknown error
(4) Pay more than return

Here also have to mention a misunderstanding, some people in order to make the program more optimized, in the analysis of business logic will be optimized to consider in, so as 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 small hints, hope to be able to help everyone better optimize the PHP code

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.