Php optimization tips – Part 1
Posted on August 12th, 2008 by Jans

- echo is faster than print.
- Wrap your string in single quotes (’) instead of double quotes (”) is faster because PHP searches for variables inside “…” and not in ‘…’, use this when you’re not using variables you need evaluating in your string.
- Use echo’s multiple parameters (or stacked) instead of string concatenation.
- Use pre-calculations, set the maximum value for your for-loops before and not in the loop. ie: for ($x=0; $x < count($array); $x), this calls the count() function each time, use $max=count($array) instead before the for-loop starts.
- Use require() instead of require_once() where possible.
- Since PHP5, the time of when the script started executing can be found in $_SERVER[’REQUEST_TIME’], use this instead of time() or microtime().
- Use <?php … ?> tags when declaring PHP as all other styles are depreciated, including short tags.
- Use isset where possible in replace of strlen. (ie: if (strlen($foo) < 5) { echo “Foo is too short”; } vs. if (!isset($foo{5})) { echo “Foo is too short”; } ).
- Use pre-calculations, set the maximum value for your for-loops before and not in the loop. ie: for ($x=0; $x < count($array); $x), this calls the count() function each time, use $max=count($array) instead before the for-loop starts.
- Use full paths in includes and requires, less time spent on resolving the OS paths.
Filed under: PHP, Tips & Tricks

Hey, good entry! I will bookmark this one! Cheers