How to optimize PHP code

WBOY
Release: 2016-07-29 09:01:22
Original
1134 people have browsed it

The purpose of PHP code optimization is to improve the execution efficiency of PHP programs. When writing PHP code, you can optimize from the following aspects:

1. Use single quotes instead of double quotes to contain strings

2. If you can define a class method as static, try to define it as static, and its speed will increase by nearly 4 times.

3. Use echo instead of print, which is more efficient

4. Log out unused variables in time, especially large arrays, to free up memory

5. Try to avoid using __get(), __set (), __autoload()

6. require_once() is expensive, use it as little as possible

7. Try to use functions instead of regular expressions to complete the same function

8. str_replace function Faster than preg_replace function

9. Using branch statements (ie switch cases) is better than using multiple if, else if statements

10. Using @ to mask error messages is very inefficient

11. When executing the increment or decrement of variable $i, ++$i will be faster than $i++

12. Try to use PHP’s built-in functions

13. Inside the loop body Do not declare variables, especially large variables

14. foreach is more efficient, try to use foreach instead of while and for loops

15. Use $i+=1 instead of $i= $i+1, more efficient

16. For global Global variables, unset() them immediately after use

17. Do some caching appropriately


The above introduces the methods of optimizing PHP code, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template