PHP: パフォーマンス改善のためにすべきこと

• Avoid  printf() when  echo is all you need.

• Avoid recomputing values inside a loop, as PHP’s parser does not remove loop invariants. For example, don’t do this if the size of  $array doesn’t change:
for ($i = 0; $i < count($array); $i++) { /* do something */ }

Instead, do this:
$num = count($array);
for ($i = 0; $i < $num; $i++) { /* do something */ }

• Include only files that you need. Split included files to include only functions that you are sure will be used together. Although the code may be a bit more difficult to maintain, parsing code you don’t use is expensive.

• If you are using a database, use persistent database connections—setting up and tearing down database connections can be slow.

• Don’t use a regular expression when a simple string-manipulation function will do the job. For example, to turn one character into another in a string, use str_replace() , not  preg_replace() . 


———Programming PHP 3rd edition 325P