7 Areas Of PHP You Might Want To Optimize

Oct 16th, 09 by Faez | 20 Comments |
Delicious 62 hits
You maybe interested in our latest article 5 Tips To Optimize Your Wordpress.org Blog For Search Engines.

PHP is undoubtedly one of the preferred development methods chosen by many to create dynamic web pages. As contents get bigger, there is a real need to manage our PHP codes so that it runs faster. Unfortunately for many beginners, designers and enthusiasts, making PHP codes run faster requires intricate knowledge of the overall infrastructure around PHP. It could be your Apache settings, your operating system, or even the memory of your machine.

But we can start from somewhere. Here are 7 areas of your own PHP code that you can modify for performance.

PHP static Keyword

Usage of the static keyword on functions is often programmer’s first choice for optimization. It allows methods within a PHP class to be accessed without the need to instantiate it.

<?php
class Foo {
public static function staticMethod() {
// put codes here
}
}
Foo::staticMethod();
?>

The process of instantiating an object is always expensive and as we can see here, the function staticMethod() can be called without creating a new Foo object. This optimization could lead almost up to 4 times speed improvement.

PHP switch Statements

Most programmers prefer the general if-statements, but some performance gains can be achieved if we use the more specific switch-statements, especially when we have to deal with a lot of conditions.

<?php
$value = 1;

switch ($value)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>

String Manipulation

Many casual programmers interchangeably use the single and double quotation marks in PHP. Knowing when and where to use them not only encourages proper coding technique but also faster execution in the long run. All you need to know is this, single quotation marks between a string mean that the PHP interpreter doesn’t have to scan the string for variables, as opposed to double quotation marks.

Another frequent string manipulation practice happens when we want to replace a string with another string. There are about 4 ways a programmer could achieve this, using functions sprintf, preg_replace, str_replace or strtr, albeit different low-level implementations between them. It is enough for us to know that using strtr whenever possible takes lesser time to complete than the other three, almost by a factor of 4.

Data Structures

With PHP 5 comes support for Object Oriented Programming (OOP). While OOP does promote scalability while minimising the amount of code you write, not everything has to be in OOP. Often these OOP practices, such as creating a new object or calling methods within it take a massive overhead and a lot of memory. For example, lists might be useful to store data, but arrays in PHP work the same way too. Assess how big your PHP project would be – my bet is if it is just for your personal website, you might want to stick to simpler methods.

Variables

Variables in a PHP code can be declared in many places within the entire scope, some can be a local variable contained within a method or a global variable, declared outside of the scope of methods within a PHP class. When it is not required of you to use global variables, change them as local variables as this could bring almost as much as double speed improvements.

Remember OOP? Incrementing an object property for example, is 3 times slower than a local variable.

PHP Output

Like string manipulation, there are multiple ways to output results. The general consensus point that echo is faster than print. Although the justification is pretty academic, it comes down to the design between echo and print in PHP. The former does not return a value while the latter does.

Error Handling

Although error handling could promote robustness, it is better to ensure that such erroneous outcomes never happen. Error handling, mostly used during debugging purposes, is an expensive process within PHP. For instance, avoid using the @ operator to suppress errors.

We regularly update articles about resources, tutorials and Wordpress to help designers and developers. If you are new to WebDesignBooth, why not subscribe to our RSS feed and get the latest updates immediately. You can also subscribe through email or follow us on Twitter.

Comments

  1. Mike Smith says:

    thanks for posting this. I’m saving it to delicious for later reading.

    I like the switch statements a lot. I used it when building an image voting site and it worked really well.

  2. DJ SCREW says:

    Thank you…..Bookmarked

  3. It will be nice, if you continue this article :)

  4. Another amazing post here, I’m truly impressed. If you’d ever be interested in having a small interview for my web 2.0 blog sometime, please let me know. I would feel honored to have a post on my blog with WebDesignBooth and it would be great publicity for both of us. My blog’s URL is http://www.insidethewebb.com/ so definitely check it out and let me know what you think!

  5. I love learning new stuff about php. Thanks!

  6. The String Manipulation tip is a handy one, I didn’t know that, thank you.

  7. I understand your need to save cpu cycles, but if you use static methods and mix oop and procedural programming, you had better abandon objects entirely. :)

  8. Jef says:

    Nice hint about the string manipulation, didn’t know that…

  9. WoW :) Some awesome tips provide in this article about php coding optimize. Really very helpful tips. Naturally we usually forget these stuff while coding. Thanks for sharing this nice post.

  10. Andrew says:

    Just a note about the single quotes vs. double quotes. Make sure you know that escape characters will not be processed in single quotes either.

    So while you can get a new line in double quotes with “\n”, in single quotes – ‘\n’ – it means literally “backslash n”.

    Just a little pitfall I fell into when I first learned about this.

    Also, could you clarify the point about error suppression with @? I read somewhere (can’t remember where) that using @ to suppress errors doesn’t actually have an impact on performance. It’s the error that’s costly, and the @ doesn’t change that. I haven’t run any benchmarks myself, so I don’t know. Maybe you could shed some light on the subject.

    At any rate, great article. Thanks.

  11. great article, thanks for tips..

  12. Andre says:

    Nice post, very usefull.

Leave a Comment