In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces the optimization of PHP code skills, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Summary of optimizing PHP Code skills
1. If a method can be static, it is declared to be static, and the speed can be increased by 1 to 4.
2. Echo is more efficient than print because echo does not return a value and print returns an integer
3. Set the maximum number of cycles before the loop, not in the loop
4. Destroy variables to free memory, especially large arrays
5. Avoid using magic methods such as _ _ get, _ _ set, _ _ autoload
6. Requiere_once () consumes more resources
7. Use absolute paths in includes and requires, so it takes less time to analyze paths
8. If you need the time from sexinsex to script execution, $_ SERVER ['REQUSET_TIME'] is better than time ()
9. If you can use character processing functions, use them as much as possible, because they are more efficient than regular; / /
10. Str_replace character substitution is faster than regular replacement preg_replace, but strtr is faster than str_replace.
11. If a function can accept both arrays and simple characters as parameters, such as character substitution, and the argument list
Not too long, you can consider using more concise substitution statements, replacing only one character at a time instead of accepting arrays
As a parameter to find and replace. Make a big deal small, 1: 1 > 2
twelve。 Masking errors with @ slows down the script
13. The speed of $row ['id'] is 7 times faster than that of $row [id]. It is recommended to form the habit of adding quotation marks to array keys.
14. Error messages are useful
15. Don't use functions in loops, such as For ($x0,$ x)
< count($array); $x), count()函数在外面先计算; 16. 建立一个全局变量要比局部变量要慢 2 倍; 17. 建立一个对象属性(类里面的变量)例如($this- >Prop++) is 3 times slower than local variables
18. Establishing an undeclared local variable is 9-10 times slower than an initialized local variable.
19. Specifying a global variable that has not been used by any function will also degrade performance (the same number of local changes as the declaration
PHP may check whether this global variable exists
20. The performance of methods has nothing to do with the number of methods defined in a class, because I add 10 or more methods
There is no difference in performance after going to the tested class (these methods are before and after the test method).
21. The performance of methods in subclasses is better than that in base classes.
twenty-two。 A function that calls only one parameter and the function body is empty takes 7-8$ localvar++ operations, while
A similar method (a function in a class) runs equal to about 15$ localvar++ operations
23. It is faster to use commas instead of dots when outputting strings. Note: this only works on echo, and this function can take
Accept some strings as parameters
24. In an apache server, a php script page takes at least 2-10 times more than the corresponding HTML static page generation.
It is recommended to use more static HTML pages and a small number of steps
25. Unless you have a cache installed, your php script needs to be recompiled every time it is accessed. It is recommended to install a php
Cache programs, which can significantly improve your performance by 20-100% by removing some repetitive compilations
twenty-six。 It is recommended to use memcached, a high-performance distributed in-memory object caching system to improve the performance of dynamic network applications.
Lighten the burden on the database
twenty-seven。 Use the ip2long () and long2ip () functions to convert IP addresses into integers and store them in the database instead of characters. It can almost be lowered.
Storage space as low as 1x4. At the same time, it is easy to sort and quickly find addresses.
twenty-eight。 Use checkdnsrr () to confirm the validity of some email addresses through the existence of the domain name. This built-in function ensures that
Each domain name corresponds to an IP address
twenty-nine。 If you are using php5 and mysql4.1 or above, consider using mysqli_*, an improved function of mysql_*
thirty。 Try to like using the ternary operator (?:)
thirty-one。 Before you want to completely redo your project, see if PEAR has anything you need. PEAR is a huge repository.
Many php developers know that.
thirty-two。 Use highlight_file () to automatically print a well-formatted copy of the page source code
thirty-three。 Use the error_reporting (0) function to prevent potentially sensitive information from being displayed to the user. The ideal error report should be
Completely disabled in the php.ini file. But if you are using a shared virtual host, you cannot modify php.ini.
Then you'd better add the error_reporting (0) function on the first line of each script file (or use the
Require_once () to load) this effectively protects sensitive SQL queries and paths from being displayed in the event of an error
thirty-four。 Use gzcompress () and gzuncompress () to compress (decompress) large-capacity strings before saving (fetching)
According to the library. This built-in function can be compressed to 90% using the gzip algorithm
thirty-five。 A function has multiple return values through a reference to the address of a parameter variable. You can add a "&" to the variable to indicate
Pass by address rather than by value
thirty-six。 Using strlen () is not very fast because you have to call some other operations such as lowercase and hash table queries
We can use isset () to achieve similar functions. Isset () is faster than strlen ().
Thank you for reading this article carefully. I hope the article "what are the skills of optimizing PHP code" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you 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.