Home >Backend Development >PHP Tutorial >PHP performance optimization (2) - PHP code writing habits optimization_PHP tutorial
The following summarizes 18 optimization habits that can be done when writing php code to maximize the speed of PHP code execution!
(1) Using static static methods is 4 times faster than ordinary methods
(2) echo output is faster than print
(3) Use , instead of .
for connection characters
(4) Take the maximum value before looping instead of taking the value inside the loop
The correct way
$max = count($array);
for ($i=0;$i<$max;$i++) {
echo $i;
}
Wrong method
for ($i=0;$i
}
(5) Use unset to release the given variable
(6) Includes and requires include files using full paths
(7) Use strncasecmp, strpbrk and stripos instead of regex
(8) Use switch instead of if else statement
(9) The performance of suppressing the error symbol @ is very low
(10) Remember to close unnecessary database connections at any time
(11) $row['id'] is 7 times faster than $row[id]
(12) Adding a global variable is 2 times slower than adding a local variable
(13) Use single quotes instead of double quotes to quote the character
(14) Using HTML is 2-20 times faster than PHP scripts
(15) Using PHP cache can speed up performance by 25%-100%
(16)$++ is slower than ++$i
(17) Don’t overuse OOP, only use it in moderation
(18) Try to use PHP built-in functions
If you have anything to add, you are welcome to join