Tips & Tricks

PHP coding tips for Performance Improvement

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.

Shares:
  • […] This post was mentioned on Twitter by junichi_y, Avinash Zala and Avi, Avi. Avi said: Xpert Developer | PHP coding tips for Performance Improvement http://goo.gl/fb/uCceu […]

    Reply
  • Summer Raper
    October 23, 2010 at 6:01 am

    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.

    Reply
  • Alister
    Alister
    March 30, 2011 at 4:41 am

    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.

    Reply
  • Larry Battle
    March 30, 2011 at 6:21 am

    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?

    Reply
  • wese
    wese
    March 30, 2011 at 11:57 am

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

    Reply
  • AoSiX
    AoSiX
    March 30, 2011 at 12:38 pm

    I feel like a good programmer thank to you

    Reply
  • Walter
    March 30, 2011 at 2:08 pm

    Very good list!!!
    Thanks

    Reply
  • André
    March 30, 2011 at 9:52 pm

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

    Reply
  • Mike
    March 30, 2011 at 10:09 pm

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

    Reply
  • Michael
    Michael
    March 30, 2011 at 11:20 pm

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

    Reply
    • baptiste deleplace
      baptiste deleplace
      January 3, 2013 at 11:31 pm

      true!

      Reply
  • andrei
    andrei
    March 30, 2011 at 11:57 pm

    Very nice article!

    Reply
  • Daniel
    Daniel
    March 31, 2011 at 12:35 am

    Awesome list!

    Reply
  • Anthony
    Anthony
    March 31, 2011 at 3:51 am

    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.

    Reply
  • Tim
    Tim
    March 31, 2011 at 8:48 am

    Can someone elaborate on the WEB_ROOT usage for me?

    Reply
    • Sean Sandy
      April 1, 2011 at 8:08 pm

      He means instead of including like this:

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

      do this:

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

      Reply
  • Tali
    March 31, 2011 at 12:28 pm

    Awesome little, looking forward to the next one.

    Reply
  • Julio
    Julio
    March 31, 2011 at 4:20 pm

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

    Reply
  • ckdo
    March 31, 2011 at 9:19 pm

    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.

    Reply
  • […] PHP coding tips for Performance Improvement […]

    Reply
  • thanhbui
    May 24, 2011 at 3:34 pm

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

    Reply
  • Reazul
    September 8, 2011 at 10:40 pm

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

    Reply
  • […] my earlier article on PHP Coding Tips, I have mentioned that static methods are 33% faster than normal […]

    Reply
  • haris
    April 17, 2012 at 5:46 am

    nice post i like this

    Reply
  • shrikant salunke
    shrikant salunke
    June 4, 2012 at 1:15 pm

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

    Reply
  • Mayank
    Mayank
    June 5, 2012 at 11:26 pm

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

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *