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

How to improve the performance of PHP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail how to improve PHP performance, Xiaobian feels quite practical, so share it with you as a reference, I hope you can gain something after reading this article.

1. It is faster to enclose strings in single quotes than double quotes. PHP searches for variables in double-quoted strings, not single-quoted strings.

2. If you can define a class method as static, try to define it as static, it will be nearly four times faster.

3.$ row ['id '] is seven times faster than $row[id].

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

5. Determine the maximum number of loops before executing the for loop. Don't calculate the maximum value every time. It's better to use foreach instead.

6. Unregister unused variables, especially large arrays, to free up memory.

7. Try to avoid__get,__set,__autoload.

Request_once () is expensive.

9. Use absolute paths when you include files, because it avoids the speed of PHP searching for files in include_path and takes less time to parse operating system paths.

10. If you want to know when the script starts executing, use $_SERVER ['REQUEST_TIME'] rather than time().

11. Functions perform the same function instead of regular expressions.

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

13. If a string substitution function accepts arrays or characters as arguments, and the arguments are not too long, consider writing an extra substitution code so that the arguments are passed one character at a time, rather than just one line of code that accepts arrays as arguments for queries and substitutions.

14. Using a switch case is better than using multiple if, else if statements.

15. The practice of masking error messages with @ is very inefficient, extremely inefficient.

16. Open mod_deflate module of apache to improve browsing speed of web pages.

17. Database connections should be closed when used up, do not use long connections.

18. Error messages are expensive.

19. Incrementing local variables in methods is the fastest. Almost as fast as calling local variables in a function.

20. Incrementing a global variable is twice as slow as incrementing a local variable.

21. Incrementing an object attribute (e.g.,$this->prop++) is three times slower than incrementing a local variable.

About "how to improve PHP performance" this article is shared here, I hope the above content can be of some help to everyone, so that you can learn more knowledge, if you think the article is good, please share it to let more people see.

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

Development

Wechat

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

12
Report