When we write the program,Want to minimize your program's use of resources,Runs faster,Less code.PHP's unique syntax mixes C, Java, Perl, and PHP's innovative syntax.It can execute dynamic web pages faster than cgi or perl.PHP is embedded in the HTML document to execute,The execution efficiency is much higher than cgi which generates html tags completely.
The purpose of php optimization is to get the fastest running speed and the easiest to maintain code at the least cost.Provide everyone the following optimization tips:
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,Don't calculate the maximum every cycle,It is better to use foreach instead.
4. For global variables, unset () should be used when it is used up.
5. Use single quotes instead of double quotes to include strings,This will be faster.Because php searches for variables in double-quoted strings,Single quotes don't.
6. Functions replace regular expressions to accomplish the same function.
7. When the increment or decrement of the variable $i is performed,$i ++ will be slower than ++ $i. This difference is specific to PHP and does not apply to other languages.++ $i is faster because it only requires 3 instructions (opcodes), and $i ++ requires 4 instructions. Post-increment actually produces a temporary variable,This temporary variable is then incremented.The pre-increment is directly incremented on the original value.
8. Using a 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 must say that var_dump should be the target you are looking for,This command can meet all your needs in displaying PHP information,And most of the debugging code is related to getting the value in PHP.
10. Use full path when including files,It will take less time to resolve the operating system path.
11. It is a bad practice to create global values at any time.But sometimes it really needs to be done.It is 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, it is better to store your global variables in a config.php file.
12. If i want to know when the script starts executing,Using $_server ["request_time"] is better than time ().
13. Open apache's mod_deflate module.
14. Blocking error messages [email protected] very inefficient.
15. Use as many built-in PHP functions as possible.
16. Incrementing an undefined local variable is 9 to 10 times slower than incrementing a predefined local variable.
17. Methods in derived classes run faster than the same methods defined in the base class.
18. Define only one local variable without calling it in a function,Will also slow down (the extent is equivalent to incrementing a local variable)
19. It takes 2 to 10 times slower to parse a php script than to parse a static html page. Try to use more static html pages and fewer scripts.
20. As mentioned before,99%of the most important part of any PHP website is probably the database.Therefore, you need to be very familiar with how to use SQL properly, learn about associated tables and more advanced database technologies.
21. Calling an empty function with one parameter,The time it takes is equivalent to performing a local variable increment operation 7 to 8 times.
22. When manipulating a string and need to check whether its length meets certain requirements,You would expect to use the strlen () function. This function executes fairly quickly,Because it doesn't do any calculations,Return only the zval structure (c's built-in data structure,Used to store the known string length in php variables).
23. Object-oriented development is not necessary in all cases,Object-oriented is often expensive,Each method and object call consumes a lot of memory.
24. Unless scripts can be cached,Otherwise it will be recompiled every time it is called.Introducing a set of PHP cache mechanisms can usually improve performance by 25%to 100%to avoid compilation overhead.
Finally, let me remind you of things you should pay attention to when optimizing your code:
Short code is not equal to fast code
Many people want to keep the code as concise as possible when writing programs.But shorter code sometimes takes longer to execute.
2. When writing a program, we should pay more attention to the scalability of the program.Rather than pursue speed.
3. Before optimizing your code,First look at the database-related part,Because most applications have a bottleneck in the database, not the code.
4. Micro-optimization is worthless
What is called micro-optimization?As mentioned earlier, replace the regular expression code with string functions.This has the following disadvantages:
(1) It takes a long time
(2) Will not fundamentally solve performance problems
(3) It is very likely to break the previous code and cause unknown errors
(4) Pay more than pay
I have to mention a misunderstanding here,In order to make the program more optimized,Considering optimization when analyzing business logic,To change the business logic in order to get better code.This is a very stupid idea,Because the purpose of the program is to deal with problems encountered in reality,We cannot put the cart before the horse.
- 50 Practical Tips for High-Quality PHP Code (Part 2)
- 50 practical tips for high-quality PHP code (on)
- Implementation of short code and related functions in WordPress development
- Ruby's 25 programming details (tips, practical code snippets)
- Collection of 60 very useful jQuery code development tips
- 30 classic jQuery code development tips
- Bash shell programming tips to make the code clean and clear
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- xcode - pod install [!] no `podfile 'found in the project directory
- dart - flutter: the instance member'stars' can't be accessed in an initializer error