php code writing habits optimization summary

  • 2020-06-15 07:55:50
  • OfStack

(1) Using static static method is 4 times faster than ordinary method
(2) echo output is faster than print
(3) Use of concatenation characters instead.
(4) Take the maximum value before the loop, instead of taking the value inside the loop
The right way
$max = count($array);
for ($i=0;$i < $max;$i++) {
echo $i;
}
The wrong way
for ($i=0;$i < count($array);$i++) {
echo $i;
}
(5) Use unset to release the given variable
(6) includes and requires include files using full paths
(7) strncasecmp, strpbrk and stripos are used instead of regex
(8) Use switch instead of if else statement
(9) The performance of the suppression error @ is very low
(10) Remember to close unwanted database connections at any time
(11) $row['id'] is seven times faster than $row[id]
(12) Increasing a global variable is 2 times slower than increasing a local variable
(13) Use single quotes instead of double quotes to refer to characters
(14) Using HTML is 2-20 times faster than PHP script
(15) Using PHP cache can accelerate the performance by 25-100%
$++ is slower than ++$i
(17) Don't overuse OOP. Use moderation
(18) Try to use the built-in functions of PHP


Related articles: