
PHP is a very complex language and unfortunately at times it can be quite slow. Here are some easy things you can do that can speed up your application.
Stop using foreach()
foreach() has been proven by several benchmarks to be extremely slow. Do not use it under any circumstances. Here’s an example of an inefficient piece of code which uses foreach():
$myArray = array(1, 2, 3, 4);
foreach ($myArray as $value) {
echo $value;
}
Instead, just use regular for.
$myArray = array(1, 2, 3, 4);
$amount = count($myArray);
for ($i = 0; $i <= $amount; $i++) {
echo $myArray[$i];
}
They do exactly the same thing, but the second one is much faster.
Avoid Double Quotes
Double quotes (” “) should only be used when needed, otherwise you should use single quotes. Double quotes search for variables within the string which takes up time; and if there’s no variables within the string then it’s pointless to use double quotes.
Use echo Efficiently
If you do this:
echo "Hello" . $myVar;
PHP has to concatenate “Hello” and the value of $myVar, and then output it.
However, if you use a comma like this:
echo "Hello", $myVar;
PHP does not have to concatenate the “Hello” and $myVar, instead it just outputs both of them.
Calculate the array size before hand
I often see code like this:
$myArray = array(1, 2, 3, 4);
for ($i = 0; $i <= count($myArray); $i++) {
echo $myArray[$i];
}
The problem? Every iteration $myArray is recounted. This is a huge waste of time. Instead, use this:
$myArray = array(1, 2, 3, 4);
$amount = count($myArray);
for ($i = 0; $i <= $amount; $i++) {
echo $myArray[$i];
}
Install APC
APC, or Alternative PHP Cache is a free addon to PHP which speeds up PHP by caching its opcodes. I highly recommend it for any site with a large audience.
So there you have it, 5 easy ways to make your application run faster, I hope these helped you and make sure to leave a comment!

6 Comments
I didn’t know that about foreach. Thanks.
Very useful!
I think you need to do one of three things…check your assertions, specify your PHP version, or provide benchmarks. For example, according to the most comprehensive set of benchmarks I’ve seen, a foreach loop is actually the fastest way to loop an array in current versions of php…unless you are modifying the array itself within the loop, in which case it is pretty much the slowest. However, different sources seem to say much different things about that one.
Also, something I didn’t know until I looked it up: the difference between single quotes and double quotes is almost never going to be significant as long as you escape any actual dollar signs in a double-quoted string (to keep php from trying to interpret them as variables).
See http://www.phpbench.com/ for benchmarks.
Maybe the difference is minimal between the quotes but it wouldn’t hurt using single quotes, would it?
> I often see code like this:
Yes, you’ve used the same xample a couple of lines higher. Anyway, use:
for ($i = 0, $e = count($array); $i <= $e; $i++) {
instead. Much less clutter.
Also, could you elaborate on why is foreach slower than for? Only thing I can think of is that foreach makes a copy of the array while for does not. But that can be changed with:
foreach($array as &$reference)
Witch will not make a copy of the array but add an reference to the content. In addition foreach can be used on objects that inmplement the Iterator interface, while for wouldn't work here.
I really am unsure why it is slower but benchmarks prove it is. Who said reusing code was cheating?
I’d imagine using
for ($i = 0, $e = count($array); $i < = $e; $i++) {would be even slower since it's not only counting each iteration but it's also setting $e.Great points. I can always trust finding thought provoking content here. Keep up the greater work! I look frontward to more!
One Trackback
Optimizing Your PHP Apps | HTML-Tricks…
PHP is a very complex language and unfortunately at times it can be quite slow. Here are some easy things you can do that can speed up your application….