Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

20 key points to improve the efficiency of php programming

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

Sorted out 20 key points that can improve the efficiency of php programming, post a blog record, friends who need can refer to.

1. If you can define the method of a class as static, try to define it as static, which will be nearly 4 times faster.

2. The speed of $row ['id'] is seven times that of $row [id].

3. Echo is faster than print and uses multiple parameters of echo instead of string concatenation, such as echo $str1,$str2.

4. Determine the maximum number of cycles before executing the for loop. Do not calculate the maximum value every time. It is best to use foreach instead.

5. Log out unused variables, especially large arrays, in order to free memory.

6. Try to avoid using get,set,__autoload.

7. Require_once () is expensive.

8. Include files use absolute paths as far as possible, because it avoids the speed at which PHP looks for files in include_path, and it takes less time to parse the operating system path.

9. If you want to know when the script starts to execute, using $_ SERVER ['REQUEST_TIME'] is better than time ().

10. The function performs the same function instead of the regular expression.

11. The str_replace function is faster than the preg_replace function, but the strtr function is four times more efficient than the str_replace function.

12. If a string replacement function accepts arrays or characters as parameters, and the parameter length is not too long, you can consider writing an additional replacement code so that each passing parameter is one character. Instead of just writing one line of code, you can accept an array as a query and replacement parameter.

13. It is better to use a selective branch statement (switch case) than to use multiple if,else if statements.

14. The practice of blocking error messages with @ is very inefficient, extremely inefficient.

15. Open the mod_deflate module of apache to improve the browsing speed of the web page.

16. The database connection should be turned off when it is finished, and do not use a long connection.

17. Error messages are expensive.

18. It is the fastest to increment local variables in the method. It is almost as fast as calling a local variable in a function.

19. Incrementing a global variable is twice as slow as increasing a local variable.

20. Incrementing an object property (such as $this- > prop++) is 3 times slower than incrementing a local variable.

Also, it is faster to include strings in single quotation marks instead of double quotation marks. Because the PHP source code will search for variables in strings surrounded by double quotes, while single quotes will not, note: only echo can do this, it is a function that can take multiple strings as parameters. (translation Note: PHP manual says that echo is a language structure, not a real function, so the function is enclosed in double quotes).

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report