PHPBuilder - MySQL and PHP: How to make it work without killing your server

RSS Twitter

MySQL and PHP: How to make it work without killing your server

by: PHP Builder Staff
May 13, 2009

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.

Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.




(Maximum characters: 1200). You have characters left.