There are currently over 25 Million websites powered by PHP. If you had the time (and patience, nevermind the means) to check all of them, you would most certainly find that most use PHP in conjunction with a MySQL Database. The reasons for this is simple: they work extremely well together, they are relatively simple to integrate and they are flexible.
But in PHP's flexibility lies its greatest weakness. Just because it allows you to do almost anything you like in just about any way you like, does not mean that you are code is performing at it's best. If it has not happened to you already, the day will come that you start to ask yourself why your script is so slow, even on localhost!
Until you properly understand what happens in the background--on the server--while your code is being processed, you will not have the tools to eliminate slow server responses. I am not, however, talking about the actual machine process that happens on the server. I am simply talking about what happens when you type echo, or print, or foreach. What are you telling PHP to do?
Just before we actually get into it, a little note: This article assumes a basic level of understanding of PHP and MYSQL. We will not be rooting through the basics of coding before getting to our points. However, I will try to keep things as simple as possible as this topic is relative to developers of all levels.

PHP: Print, Echo, Strings, Variables and Loops
You will have undoubtably heard of the cardinal rule regarding echo vs. print. Certainly, I dont think any discussion on server resources would be complete without it. Its simple:

    print $variable;

is slower than

    echo $variable;

However, this difference is neglegible. My own script timer could not accurately time one faster than the other in a script that printed a simple string ten thousand times.
The crux of the matter lies in the fact that print always returns true, so that you can do things like:

    $ret = print 'hello world';

and $ret will be 1. You can also do things like

    $b ? print "true" : print "false";

According to the PHP Manual, print is also part of the precedence table which it needs to be if it is to be used within a complex expression. It is just about at the bottom of the precedence list though. Only "," AND, OR and XOR are lower. In my mind the much more important fact comes in use: print can only pass one paramater, where echo can pass multiple parameters. So, you could say:

    echo $variable, $variable2, $variable3, $variable4;

but for the equivalent thing in print:

    print "{$variable} {$variable2} {$variable3} {$variable4}";

Thus, it does not really matter which one you use, although the fact that print actually returns true, every time it is used, does seem like a waste of resources to me. If all you are trying to do is simply print some text to the screen, use echo.
On the topic of strings, it is a well known fact that the following examples are all valid:

    echo "Hello World, this is my list: {$variable}, {$variable2}, {$variable3}, {$variable4}.";
    echo "Hello World, this is my list: ".$variable.", ".$variable2.", ".$variable3.", ".$variable4.".";
    echo 'Hello World, this is my list: '.$variable.', '.$variable2.', '.$variable3.', '.$variable4.'.';

but the fastest, lightest way of doing this is the third example. This is because PHP does not bother trying to parse anything inside single quotes, so it simply ignores the string part and just echoes the contents to the screen without really caring what they are. Then it parses the values outside of the single quotes normally. Again, these results are negligable should you be using a very small page, but on monolith frameworks that load something like 500 pages per individual pageload, these things certainly do add up.
Something to remember regarding for loops is that the maximum boundary should be calculated and stored inside a variable like this:

    $max = count($var);

    for($i=0; $i < $max; $i++)
      // do something

rather than

    for($i=0; $i < count($var); $i++)
      // do something

Because in the second instance the maximum boundary is calculated through every iteration of the loop, severely wasting server resources.
Something else I would like to touch on lightly before moving on is that it is heavier on server resources to keep switching php on and off inside a script than to just print or echo the html. So, instead of this:

    if(isset($var)){ ?>
      <p>hello world</p>

just do the following:

       echo '<p>hello world</p>';

The idea is that opening and closing PHP twice uses more resources than just echoing a string once.

MySQL queries probably slow down your pageloads more than anything else. A big server-killer comes from queries inside loops--sometimes unavoidable but in most cases there are things that could be done to lighten the server load.

A simple class
When you have built enough websites you will see that you use many things over and over again. At some point it just becomes simpler to chuck that into a class and store it in some sort of archive that way you can dip into whenever you lay out the structure of a new site. Being an OOP junkie, I have a simple class that first defines the array which creates my database connection.

File: dbConfig.php


  Class dbConfig
    public $config = array(
    'db_server'    => 'server_name',
    'db_name'      => 'database_name',
    'db_username'  => 'username',
    'db_password'  => 'password'

And then I keep a database class in my site library that references these configs and in turn supplies the site with a database connection as well as a few inbuilt functions that make sure my code is always perfect no matter what I do. The simplest version of my database class follows here:

File: db.php


  Class db extends dbConfig
    public $connection;

    public function __construct()

    public function connect()

    function updateTable($table, $field, $value, $id)
      return mysql_query("update {$table} set {$field} = '{$value}' where id = {$id}", $this->connection);

    function deleteFromTable($table, $id)
      return mysql_query("delete from {$table} where id = {$id} limit 1", $this->connection);

    function getArray($query)
      if($result = @mysql_query($query, $this->connection))
        while($tmp = @mysql_fetch_array($result))
          $dbarray[]= $tmp;

        if (isset($dbarray))
          return $dbarray;
        else return NULL;

    function query($query)
      return mysql_query($query, $this->connection);

  $db = new db;

In essence, this gives me a very simple database framework that allows me to manage the simple queries I need to do on the site. Since I have started working with MVC Frameworks, however, I have found that this way is not really workable. But for procedural coding it still works well. Simply include both the dbConfig.php and db.php files in your includes file or make sure your autoloader knows where to find them and you are set.

Data is data
When you get to the point that you are using the same data over and over on the same page, the time has come to save it into an array which can be referenced over repeatedly without having the resource of query after query wasted.
Begin by building the query from what you know you will need from the database. Let us assume we are going to print out user profile information for a specific user from the user table of our database.

  $query = $db->getArray("select id, name, surname, age, email, country, city from user where id=6 limit 1");

You will have noticed the following from my code:

I did not use select *
This is important. If you dont need every single field from the database, dont waste server time by asking for it. Select only the fields you need.

I used limit 1
This speeds your query up nicely. If you are only looking for a single row as a result, using limit 1 simply tells mysql to stop looking once it has found a result. Not doing so means that MySQL will continue looping through the table anyway, wasting time and resources for no reason.
Now I have all the table data I will require safely stored in an array that I can reference any time I need it. The format of the array will look like this:

  $id      =$query[0]['id'];
  $name    =$query[0]['name'];
  $surname =$query[0]['surname'];
  $age     =$query[0]['age'];
  $email   =$query[0]['email'];
  $country =$query[0]['country'];
  $city    =$query[0]['city'];

In a similar vain, if we wanted to list all of the users in a list of lets say name, surname and age, we could do the following:

  $query = $db->getArray("select name, surname, age from user order by name");

This query will return an array that can be manipulated as follows:

  foreach($query as $user){
  echo $user['name'].' '.$user['surname'].' '.('. $user['age'] .')';
  echo '<br />';

The above will print a neat list like this:

John Arbuckle (32)
Kevin Smith (28)
Mike Carrigan (43)

And of course you could do tons more with that array, and you should not need much more than that in order to get the results you need.

In Conclusion
In this article we explored a few techniques that will not only improve our coding style, but help to make our scripts execute faster. We also had a short peak at a database management class that we will hopefully be able to have a closer look at in the near future. Part of our database optimization showed how to store database results in an array, thereby making scripts execute faster because there is no need for repeat queries. We also had a brief look at how to manipulate the resulting arrays.

Until next time, Good Luck!

Marc Steven Plotz