7 Areas Of PHP You Might Want To Optimize

Oct 16th, 09 by Faez | View Comments |
You maybe interested in our latest article Personal Facebook Fan Pages of Celebrities.

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.
  • http://www.madebyguerrilla.com Mike Smith

    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.

  • http://www.texastakeover.com DJ SCREW

    Thank you…..Bookmarked

  • http://codehands.com Miroslav Nikolov

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

  • http://www.insidethewebb.com/ Inside the Webb

    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!

  • http://complimedia.com Montana Flynn

    I love learning new stuff about php. Thanks!

  • http://webdeveloper2.com Dave Kinsella

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

  • http:/giorgiosironi.blogspot.com Giorgio Sironi

    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. :)

  • http://toproundups.com/2009/10/16/7-areas-of-php-you-might-want-to-optimize/ 7 Areas Of PHP You Might Want To Optimize | TopRoundups

    [...] 7 Areas Of PHP You Might Want To Optimize Submitted by Guest [...]

  • http://www.jeremymbryant.com/huge-list-of-links-submitted-by-users-of-tripwire-magazine/ Huge List Of Links Submitted By Users Of Tripwire Magazine | Dodging Spiderwebs

    [...] 7 Areas Of PHP You Might Want To Optimize [...]

  • http://somebodysomeone.org Jef

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

  • http://www.clippingimages.com clippingimages

    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.

  • http://andrewensley.com/ Andrew

    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.

  • http://cssbrigit.com/CNews/entrada/2009101917030254 CSS Brigit | 7 Areas Of PHP You Might Want To Optimize

    7 Areas Of PHP You Might Want To Optimize…

    This article discusses 7 ways to optimize your PHP so that your can increase your website performance….

  • http://www.tripwiremagazine.com/news/news/70-great-fresh-community-links-for-designers-and-developers.html 70+ Great Fresh Community Links for Designers and Developers | tripwire magazine

    [...] 7 Areas Of PHP You Might Want To Optimize [...]

  • http://design-newz.com/2009/10/25/7-areas-of-php-you-might-want-to-optimize/ 7 Areas Of PHP You Might Want To Optimize | Design Newz

    [...] 7 Areas Of PHP You Might Want To Optimize [...]

  • http://lewayotte.com/2009/10/25/notable-tech-posts-2009-10-25/ Notable Tech Posts – 2009.10.25 | The Life of Lew Ayotte

    [...] 7 areas of PHP you might want to optimize [...]

  • http://joyoge.com joyoge bookmark

    great article, thanks for tips..

  • http://singlefunction.com/7-areas-of-php-you-might-want-to-optimize/ 7 Areas Of PHP You Might Want To Optimize | SingleFunction

    [...] Vist Source [...]

  • Andre

    Nice post, very usefull.

  • http://www.bizimhost.biz/wordpress/7-tools-to-optimize-the-speed-of-your-website-2/ 7 Tools To Optimize The Speed of Your Website | Bizimhost.biz

    [...] 7 Areas Of PHP You Might Want To Optimize [...]

  • http://www.milcoins.com milcoins

    As it seems I now need to update some of my PHP , Thanks for the info.

  • http://www.roommagumma.com Alex

    nice job !
    GREETING FROM ITALY

  • http://www.twicethespeed.com/ how to run faster

    Arguably the biggest myth in all of sports says you can’t coach speed, teach athletes how to run faster or make serious improvements to multidirectional speed and agility or explosive strength and power.

blog comments powered by Disqus