In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
38 tips to optimize PHP code are what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
1. If a method can be static, it is declared to be static, and the speed can be increased by 1.
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 ones.
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 parameter list is not too long, you can consider using more concise replacement statements, replacing only one character at a time. Instead of accepting arrays as search and replacement parameters. Make a big deal small, 1: 1 > 2
12. Masking errors with @ will slow 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 the loop, such as For ($x0,$ x)
< count($array); $x), count()函数在外面先计算; 16、在方法里建立局部变量速度最快,97xxoo几乎和在方法里调用局部变量一样快; 17、建立一个全局变量要比局部变量要慢2倍; 18、建立一个对象属性(类里面的变量)例如($this->Prop++) is 3 times slower than local variables
19. Establishing an undeclared local variable is 9-10 times slower than an initialized local variable
20. Declaring a global variable that has not been used by any function also degrades performance (just like declaring the same number of local variables). PHP may check whether the global variable exists.
21. The performance of a method has nothing to do with the number of methods defined in a class, because there is no difference in performance after I add 10 or more methods to the tested class (these methods are before and after the test method)
22. The performance of methods in subclasses is better than that in base classes
23. 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 the class) runs about 15$ localvar++ operations
24. Surrounding your string by 'instead of "will make things interpret a little faster since php looks for variables inside"... "but not inside'...'. Of course you can only do this when you don't need to have variables in the string.
25. It is faster to use commas instead of dots when outputting strings. Note: this only works on echo, and this function can accept some strings as arguments.
26. A php script page in an apache server takes at least 2-10 times longer than the corresponding HTML static page. It is recommended to use more static HTML pages and a small number of steps.
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 program, which can significantly improve your performance by 20-100% by removing some duplicate compilations.
28. It is recommended to use memcached, a high-performance distributed in-memory object caching system, to improve the performance of dynamic network applications and reduce the burden on databases.
29. Use the ip2long () and long2ip () functions to convert IP addresses into integers and store them in the database instead of characters. This can almost reduce the storage space of 1 pound by 4. At the same time, it is easy to sort and quickly find addresses.
30. Use checkdnsrr () to confirm the validity of some email addresses through the existence of domain names. This built-in function ensures that each domain name corresponds to an IP address.
31. Try to use the ternary operator (?:)
32. Before you want to redo your project completely, see if PEAR has anything you need. PEAR is a huge repository, as many php developers know.
33. Use highlight_file () to automatically print a well-formatted copy of the page source code
Use the error_reporting (0) function to prevent potentially sensitive information from being displayed to the user. Ideal error reporting should be completely disabled in the php.ini file. But if you are using a shared virtual host and you cannot modify php.ini, you'd better add the error_reporting (0) function on the first line of each script file (or load it with require_once ()). This can effectively protect sensitive SQL queries and paths from being displayed in the event of an error.
Use gzcompress () and gzuncompress () to compress (decompress) large-capacity strings when saving (taking out) the database. This built-in function can be compressed to 90% using the gzip algorithm
36. Make a function have multiple return values by referencing the address of the parameter variable. You can add a "&" to the variable to indicate that it is passed by address rather than by value.
37. Fully understand "magic quotes" and the dangers of SQL injection. I'm hoping that most developers reading this are already familiar with SQL injection. However, I list it here because it's absolutely critical to understand. If you've never heard the term before, spend the entire rest of the day googling and reading.
38. Use strlen () because you have to call some other operations such as lowercase and hash table query, so the speed is not very good. We can use isset () to achieve similar functions. Isset () is faster than strlen ().
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.