Optimizing PHP scripts

Written by on July 13, 2007 in PHP - 2 Comments

This post is mainly for new programmers and web developers, but advanced programmers also can make use of it. While browsing forums i often see programmers asking questions about their scripts why doesn’t they work mainly, don’t get me wrong i have nothing against asking questions, but i noticed something else what concerned me, most of newbies do not optimize their scripts, as long as they do not have much visits on their websites it is ok, they can get away with it.

However it is good to learn the best programming patterns when you start learning because later it is much harder to change your habits. In this post i will list the most common mistakes i see on web development forums and websites.

First one that comes to my mind is: unintended nested loop, just look at the code:

for($i=0; $i

Do you see where nested loop is? Whenever script tries to end loop he needs to check if $i

The better solution would look like that:

for($i=0, $x=count($x_array); $i<$x; $i++)
{
    // do something
}

All we need to do is assign count($x_array) to variable and we eliminated nested loop.

Second common mistake is use of unneeded variables. This is caused by PHP, as it do not require to define new variables so web developers create new variable whenever they THINK they need one.

$x = 5;
$y = $x^2;
echo $y;

Is $y really necessary here? No, because we can achieve the same effect other way.

$x=5;
echo $x^2;

It is not only shorter script but we eliminated one variable. I know that sometimes it is hard to tell if we will need particular variable or not. To avoid this problem, before you create new variable ask yourself: Will i use this variable more then once? If yes then you can create new variable, also if you will use only once but your new var will make code look much better then you can also create new variable.

Third common web development mistake is bad use of quotes. There are two types of quotes ” and ‘,if they would do the same then there would no reason for both of them to exist. So let me explain it briefly, everything between ” and ” is parsed, it means you can use variables inside it, while text between ‘ and ‘ is not parsed, take a look:

$x = "add";
echo "Some text to $x";
echo 'Some text to $x';
// Output 1: Some text to add
// Output 2: Some text to $x

Quite simple. At the first sight it looks like it is best to always use ” quote, it seems more universal and practical, but remember PHP needs to parse double quote (“), so it needs more time to handle text between ” and “, and what we want to do is the opposite – we want to save time.

Take a look at few more examples of bad and good usage of quotes:

// bad usage
$txt1 = "$x, $y, $z";
$txt2 = "do not parse this string";
// good usage
$txt1 = $x.', '.$y.', '.$z;
$txt2 = 'do not parse this string';

These were the most common mistakes, that require some programming practice to take care of them, but there are also few mistakes that you can take care of right away.

Some of PHP functions has got aliases, do NOT ever use them, they slower your scripts, how to find out if certain function is an alias or not? Simply go to php.net and checkout this function in manual, if it will be alias then it will be written there.

The same goes to creating aliases by yourself, because you want your function to have beautiful name.

When it comes to regular expressions the most common mistake is to user preg_replace(…) or eregi_replace(..) to replace single word in a string, obviously you know there is str_replace(…) function and it should be used in such a simple task, why? Because it is at least 25% quicker then eregi_replace and preg_replace.

About the Author

Greg Winiarski is a freelance PHP and JavaScript programmer. He specializes in web applications and WordPress development.

2 Comments on "Optimizing PHP scripts"

  1. Alexey August 14, 2008 at 11:14 pm ·

    Good article!
    But first code section is not complete i guess…

  2. Radek March 2, 2009 at 12:11 am ·

    commenting usually isnt my thing, but ive spent an hour on the site, so thanks for the info

Leave a Comment