In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "PHP code optimization skills sharing", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor to take you to learn "PHP code optimization skills sharing"!
The goal of PHP optimization is to get the fastest running speed and the most maintainable code at the least cost. Here are some optimization tips:
1. Echo is faster than print.
two。 Use multiple parameters of echo instead of string concatenation.
3. Determine the maximum number of cycles before executing the for loop. Do not calculate the maximum value for each loop. It is best to use foreach instead.
4. For the global variable, you should unset () as soon as you finish using it.
5. It is faster to include strings in single quotation marks instead of double quotation marks. Because PHP searches for variables in strings enclosed in double quotes, single quotes do not.
6. Function to perform the same function instead of regular expression.
7. When the increment or decrement of the variable $I is performed, $ibasket + is slower than + + $I. This difference is unique to PHP and does not apply to other languages. + $I is faster because it requires only three instructions (opcodes) and $iTunes + requires four instructions. The post increment actually produces a temporary variable, which is then incremented. The pre-increment increases directly on the original value.
8. Using a 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 technology, 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 has to do with getting values in PHP.
10. Using the full path when including the file takes less time to resolve the operating system path.
11. It is a bad practice to create global values easily, but sometimes it is necessary to do so. 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 store your global variables in a config.php file.
twelve。 If you want to know when the script starts to execute, using $_ SERVER ['REQUEST_TIME'] is better than 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. 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. Defining only a local variable without calling it in a function also slows down (to the extent equivalent to incrementing a local variable)
19. Apache parses a PHP script 2 to 10 times slower than parsing a static HTML page. Try to use more static HTML pages and fewer scripts.
20. As mentioned earlier, the most important part of any php site is likely to be a database. Therefore, you need to be very familiar with how to use sql correctly, learn associative tables and more advanced database techniques.
21. Calling an empty function with one parameter takes the same time as performing 7 to 8 local variable increments.
twenty-two。 When you manipulate a string and need to verify that its length meets certain requirements, you take it for granted that you use the strlen () function. This function is fairly fast because it doesn't do any calculations and only returns the known string length stored in the zval structure (C's built-in data structure for storing PHP variables).
23. Object-oriented development is not necessary in all cases. Object-oriented is often very expensive, and each method and object call consumes a lot of memory.
24. Unless the script can be cached, it will be recompiled each time it is called. The introduction of a PHP caching mechanism can typically improve performance by 25 to 100 percent, eliminating compilation overhead.
Finally, one more reminder of what you should pay attention to when optimizing your code:
1. Short code is not equal to fast code.
Many people want to write code as succinctly as possible when writing programs, but shorter code sometimes takes longer to execute.
two。 When writing a program, we should pay more attention to the expansibility of the program, rather than the pursuit of speed.
3. Before you optimize your code, look at the parts related to the database, because the bottleneck of most applications is the database rather than the code.
4. The loss of micro-optimization outweighs the gain.
What is micro-optimization? As mentioned earlier, replace the regular expression part of the code with a string function. This has the following disadvantages:
(1) it takes a long time
(2) it will not fundamentally solve the performance problem.
(3) it is likely to destroy the previous code and cause unknown errors.
(4) giving is greater than return.
There is also a misunderstanding that some people take optimization into account when analyzing business logic in order to make the program more optimized, thus changing 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 the problems encountered in reality, and we cannot put the cart before the horse.
At this point, I believe you have a deeper understanding of "PHP code optimization skills sharing", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.