PHP coding tips for Performance Improvement

By Avinash

October 10, 2010Tips & Tricks29 Comments

This post covers most performance improvement tips related to PHP coding. While working with small website or project it’s ok to ignore these tips, but when you are dealing with large website or project which is going to maintained for long term and which have large number of user base. Developer must have to consider the below tips from the starting of the project. It will surely make drastic change in your website performance.

In PHP there are so many ways to perform the same task. Normally developers are using the way which is most comfortable for them or which they are more aware of.

  1. echo is more faster than print. Both functions are used for the same thing but echo is the language construct which return nothing, which print will return 0 or 1 based on success or failure.
  2. include_once is more costly than include statement. Because it have to search for class definition you are trying to include is already included or not?
  3. Always use single quotes for long strings instead of double quotes. Because in double quotes it will search for php variable to evaluate them. So in this case, echo ‘This is long string’ . $name is faster then echo “This is long string $name”. But from above both echo ‘This is long string’ , $name, is faster because it does not require any string manipulation.
  4. Don’t use for($i=0; $i<count($names);$i++){…} instead of this use $size = count($names); for($i=0; $i<$size;$i++){…}. The first method will call count function on each iteration of for loop, while in second iteration count function is being called only once.
  5. If you can declare a method as static then let it be, because its 33% more faster than member functions.
  6. If you can solve your problem without using regular expression then don’t use it. Regular expression are slower then their php counterparts. For example use str_replace instead of preg_replace.
  7. Try to minimize relative paths for files inclusion. For relative path inclusion it will search for default include path then current directory then so on. So file search in that case may take more time. Better practice is to declare the constant called WEB_ROOT which defines the root.
  8. Identical operator (===) is faster than (==) operator as identical operator will include type checking also. So If( 1 == ’1′) will return true, If( 0 == ”) will return true while if you use identical operator both this conditions If( 1 === ’1′) and If(0 === ”)  will return false. So it is recommended to use identical operator when you are going to use some boolean variables for deciding the flow/control of your application.
  9. Don’t use short tags <?=$name> and try to using <?php echo $name; > , It can create a problem for you if you are going to deploy your application on another server.
  10. Don’t use or relay on register_globals or magic quotes and read and configure your php.ini settings carefully.

I have not covered all the things here but there are lots of more optimizations tips are available, will see that in next post.

Share This Article

  • Pingback: Tweets that mention PHP coding tips for Performance Improvement | Xpert Developer -- Topsy.com

  • http://www.pixelvelez.com/ Summer Raper

    I havent any word to appreciate this post. Really I am impressed from this post. The person who create this post it was a great human. Thanks for shared this with us.

  • Alister

    Oh look. More ‘micro optimisations’ (and by ‘more’, I mean the same as the last 20 posts on the subject), most of which will be utterly lost in the noise of running a code cache like APC.

  • http://bateru.com/news/ Larry Battle

    Thanks for the list.
    Also for number 4, using a while loop should be faster than a for loop.

    What method did you use to benchmark?

  • wese

    So simple yet great tips. I honestly didn’t know about echo “string” (comma) $variable till this day.

  • AoSiX

    I feel like a good programmer thank to you

  • http://www.gis3w.it Walter

    Very good list!!!
    Thanks

  • http://tutoriaisparaweb.wordpress.com André

    Nice tips, i’ve do this things without know this tips thanks

  • http://www.neville.sk Mike

    Hi.
    Rule 4. i’m using
    for ( $i = 0, $size = count($names); $i < $size; $i++ )

  • Michael

    Micro Optimizations are simply not worth it (except the one with the for loop).
    Professional Programmers know that other things like Database Optimization etc. is a lot more important and has a bigger effect.

    Also Point 9 & 10: What has this to do with Performance? They’re good points in general for Coding standards but they have nothing to do with Performance..

    • baptiste deleplace

      true!

  • andrei

    Very nice article!

  • Daniel

    Awesome list!

  • Anthony

    Maybe it is a micro optimalisation, but you still can adapt your own standards to write new code with these tips. You don’t have to replace old code, but later, I had the feeling that the “messed” code became more noise, because they differ from the new code. But I think that would be personal.

  • Tim

    Can someone elaborate on the WEB_ROOT usage for me?

    • http://seanja.com SeanJA

      He means instead of including like this:

      include ‘../../../some_path/my_file.php’;

      do this:

      include WEB_ROOT . ‘/some_path/my_file.php’;

      • xpertdev

        Doc Root will be better to use :)

        • http://seanja.com SeanJA

          A better name, perhaps…

  • http://www.semicolon.co.za Tali

    Awesome little, looking forward to the next one.

  • Julio

    Nice indeed… but… “Foo $bar” WAS, IS and ALWAYS WILL BE faster than ‘Foo ‘ . $bar ;)
    I decided to code like this after making some benchmarks of my own.
    Micro-optimizations are good as long as they don’t create too much fuzzy code :)

    • http://seanja.com SeanJA

      That is a comma dude, but thanks for playing.

  • http://www.ckdo.net ckdo

    thank you for this tips.
    i already knew the biggest part of its but i learnt new tips, like echo ‘foo’,$var.

    Mike said a good think about for loop.

  • Pingback: A semana no mundo PHP (01/04/2011) | raphael.dealmeida

  • http://www.nhakhoabaominh.com thanhbui

    should not use in_array() function with huge data in the array. It’s very slow.

  • http://www.reazul.net Reazul

    I am very happy to know that i am following all the rules stated here without the Rule #9. :-D Thanks for the post. :)

  • Pingback: Static Keyword in PHP | Expert PHP Developer

  • http://www.crochet.tk haris

    nice post i like this

  • shrikant salunke

    Very nice article………………..Go Ahead

  • Mayank

    Thanks for such a wonderful post. It was a great read.