5 ways to speed up your PHP code

Almost every day at work I see people horizontally scaling their infrastructure to support their applications, such as adding CPU, Memory, HDD even whole servers. For the hosting company this is generally great news, as there is an increase in revenue per client.

However about 9 out of 10 times this is not actually required as generally there are some clear coding optimizations that would reduce load on the servers, and therefore remove the need for scaling out the infrastructure. Usually the issue is that your coders don’t really communicate that well with the hardware guys or don’t understand the implications of their coding structure. Hopefully this will make you more aware and therefore build more optimized and scalable applications.

1. Redundant Variables

This is probably one of the most common issues that I see in coding, generally you have some output returned from a function and the coder instantly creates a new variable to contain this information which is then processed straight away. See the example here:

$SomeVar = Function('hi');
if($SomeVar){
}

The faster and better way:

if(Function('hi')){
}

The most common reason for this is coders claiming that they might need to recall the output later in their code, and returning from the variable should be faster than running the function again. Yes this is a valid point some times, however you should never put it in because you might need it, leave it out and come back to add it if you do. Basically your probably just going to be wasting the servers memory, remember every little helps.

2. Cache (MemCache)

It’s always faster answering a question when you already know the question and the answer to that question, this is where memcache comes into our PHP applications.

The best example of this is on a forum where it says “Total Users: 35353”, for simplicity we can imagine that the figure is probably just coming from a MySQL database select on each page load.

However a much better way to do it would be perform the calculation lets say every 15min (on a cron) and then remember (memcache) the result, then just display the total figure from memory (memcache) each time the page is loaded. If we only get 2 visitors every 15min thats already cut the number of MySQL queries by half. Just remember that memcache will normally make your output out of date with reality, so only normally use it for statistic type data that doesn’t really matter too much.

3. SQL is faster

You need to remember that SQL isn’t just a data store where you insert, select, update and delete data from. It’s actually a very powerful tool for processing data. If your ever looking to perform processing such as math, data comparison, join’s etc… PHP will always lose in a race against SQL and you should use this to your advantage.

So when you select data from SQL think is this as close to my desired out put as I can get it? If it’s not make a better SQL query, this will be faster and also cut down on the amount of PHP code required.

4. ‘ is faster than “
I won’t really go into it too much here however if some text is wrapped in ” PHP will have to look through every character looking for a hidden variable.
Therefore look at the example below and understand that the second will be considerably faster.

echo "hello {$name}";
echo 'hello'.$name;

5. Trying to remember if
Also quite a simple one to understand but when you use something like “require_once” over the alternative “require” PHP has to work if the file has already been ‘required’ in the script. Needless to say there is more thinking and therefore more processing which will slow things down, so it might look good but think do you really need it?

// Slow
require_once('file.php');

// Faster
require('file.php');

Another 5 tip’s will be coming soon, make sure you subscribe/bookmark.

Technology enthusiastic with many ongoing online projects one of which is this personal blog PingBin. While also working full time within a data center designing and maintaining the network infrastructure.

Leave a reply:

Your email address will not be published.