24 genuine PHP code optimization skills, 24 genuine
The PHP code has been optimized for 24 things. I hope it will help you develop php projects. The specific content is as follows:
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.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.