当前位置:网站首页>The way to PHP advancement -- 38 tips to optimize PHP code (summary)

The way to PHP advancement -- 38 tips to optimize PHP code (summary)

2021-01-23 16:24:09 Programmer a mu

1、 If a method can be static , Then declare him static , Speed can be increased 1/4;

2、echo Is more efficient than print, because echo no return value ,print Returns an integer ;

3、 Set the maximum number of cycles before the cycle , Not in the loop ;

4、 Destroy variables to free memory , Especially large arrays ;

5、 Avoid using things like __get, __set, __autoload And so on ;

6、requiere_once() Comparative resource consumption ;

7、 stay includes and requires Use absolute path , It takes less time to analyze the path ;

8、 If you need to get the time to execute the script ,$_SERVER['REQUSET_TIME'] be better than time();

9、 Can use character processing function , Try to use them , Because efficiency is higher than regularity ;

10、str_replace Character replacement is better than regular replacement preg_replace fast , but strtr Than str_replace Fast 1/4

11、 If a function can accept both arrays and simple characters as parameters , For example, character replacement , And the parameter list is not too long , Consider using more concise substitution statements , Replace only one character at a time , Instead of accepting arrays as search and replace parameters . Make a big deal small ,1+1>2;

12、 use @ Masking errors slows down scripts ;

13、 r o w [ i d ] Than row['id'] Than row[id] Fast 7 times , It is suggested to form the habit of quoting array keys ;

14、 Error messages are useful ;

15、 Don't use functions in loops , for example For( x = 0 ; x=0; x < count( a r r a y ) ; array); x), count() Function is calculated outside first ;

16、 It's the fastest way to create local variables in a method , Almost as fast as calling local variables in a method ;

17、 It's slower to build a global variable than a local variable 2 times ;

18、 Create an object property ( Variables in class ) for example ($this->prop++) Slower than local variables 3 times ;

19、 It's slower to create an undeclared local variable than an initialized one 9-10 times ;

20、 Declaring a global variable that has not been used by any function can also degrade performance ( Just like declaring the same number of local variables ),PHP It is possible to 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 I added 10 Add one or more methods to the tested class ( These methods are before and after the test methods ) There's no difference in performance after the test ;

22、 Methods in subclasses perform better than in base classes ;

23、 A function with only one parameter and empty body takes as long as 7-8 Time l o c a l v a r + + operation , And a similar approach ( Functions in class ) Running is about 15 Time localvar++ operation , And a similar approach ( Functions in class ) Running is about 15 Time localvar++ operation ;

24、Surrounding your stringby ‘ instead of ” will make things interpret a little faster since php looks for variables inside “…” but not inside ‘…’. Of course you can only dothis when you don’t need to have variables in the string.

25、 It's faster to use commas instead of dots when outputting strings . Be careful : This is only right echo Work , This function takes some strings as arguments ;

26、 stay apache One in the server php The script page is better than the corresponding HTML Static page generation costs at least 2-10 Times of time , It is suggested to use more static HTML Pages and a little footwork ;

27、 Unless you have cache installed , Or your php Scripts need to be recompiled every time they are accessed . It is recommended to install php Cache program , This can significantly improve your performance by removing some repetitive compilations 20-100% Performance of ;

28、 Suggest using memcached, High performance distributed memory object caching system , Improve dynamic web application performance , Lighten the burden of the database ;

29、 Use ip2long() and long2ip() Function IP The address is converted into integer type and stored in the database instead of character type . It almost reduces 1/4 Storage space . At the same time, it's easy to sort and quickly find addresses ;

30、 Use checkdnsrr() Confirm part of... By the existence of domain name email Validity of address , This built-in function ensures that each domain name corresponds to a IP Address ;

31、 Try to like ternary operators (?:);

32、 Before you want to completely redo your project , have a look PEAR Is there anything you need .PEAR It's a huge resource pool , quite a lot php Developers know that ;

33、 Use highlight_file() Can automatically print a well formatted copy of the page source code ;

34、 Use error_reporting(0) Function to prevent potentially sensitive information from being displayed to users . The ideal error reporting should be completely disabled in the php.ini In the document . But if you're using a shared virtual host ,php.ini You can't modify , Then you'd better add error_reporting(0) function , On the first line of each script file ( Or use require_once() To load the ) This can effectively protect sensitive SQL Queries and paths are not displayed when errors occur ;

35、 Use gzcompress() and gzuncompress() Compression of large capacity strings ( decompression ) I'm saving ( Take out ) Database time . This built-in function uses gzip The algorithm can compress to 90%;

36、 Make a function have multiple return values through the reference of parameter variable address . You can add... To the variable “&” To represent delivery 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 withSQL injection. However, I list it here because it’s absolutely criticalto understand. If you’ve never heard the term before, spend the entire rest of the day googling and reading.

38、 Use strlen() Because some other operations are called, such as lowercase and hash Table query speed is not very good , We can use isset() To achieve similar functions ,isset() Faster than strlen();
If you like I wrote it Technical articles as well as Interview summary , Welcome to watch my video , also give the thumbs-up 、 Collection 、 Focus on I oh .
Thank you for your attention !
Many small partners always encounter some problems and bottlenecks when they are advanced , There is no sense of direction in the business code , I don't know where to start to improve , I've compiled some information about it , I hope I can help my friends , Sure Pay attention to me . And join my circle to learn and grow together 【 Architect's way 】 Click on the link to apply to join the circle

Architect's way jq.qq.com
 

 

版权声明
本文为[Programmer a mu]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210123162222109q.html