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.

About the Author: Avinash

Experienced full stack freelance web developer with a track record of producing excellent results for 11+ years. Excellent communication and collaboration skills, managing with a team of 20+ people. I’m motivated to make a positive impact on your revenue through the customer web portal development project.

29 Comments

  1. […] 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 […]

  2. Summer Raper October 23, 2010 at 6:01 am - Reply

    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.

  3. Alister March 30, 2011 at 4:41 am - Reply

    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.

  4. Larry Battle March 30, 2011 at 6:21 am - Reply

    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?

  5. wese March 30, 2011 at 11:57 am - Reply

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

  6. AoSiX March 30, 2011 at 12:38 pm - Reply

    I feel like a good programmer thank to you

  7. Walter March 30, 2011 at 2:08 pm - Reply

    Very good list!!!
    Thanks

  8. André March 30, 2011 at 9:52 pm - Reply

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

  9. Mike March 30, 2011 at 10:09 pm - Reply

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

  10. Michael March 30, 2011 at 11:20 pm - Reply

    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 January 3, 2013 at 11:31 pm - Reply

      true!

  11. andrei March 30, 2011 at 11:57 pm - Reply

    Very nice article!

  12. Daniel March 31, 2011 at 12:35 am - Reply

    Awesome list!

  13. Anthony March 31, 2011 at 3:51 am - Reply

    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.

  14. Tim March 31, 2011 at 8:48 am - Reply

    Can someone elaborate on the WEB_ROOT usage for me?

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

      He means instead of including like this:

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

      do this:

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

  15. Tali March 31, 2011 at 12:28 pm - Reply

    Awesome little, looking forward to the next one.

  16. Julio March 31, 2011 at 4:20 pm - Reply

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

    • Sean Sandy April 1, 2011 at 7:54 pm - Reply

      That is a comma dude, but thanks for playing.

  17. ckdo March 31, 2011 at 9:19 pm - Reply

    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.

  18. […] PHP coding tips for Performance Improvement […]

  19. thanhbui May 24, 2011 at 3:34 pm - Reply

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

  20. Reazul September 8, 2011 at 10:40 pm - Reply

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

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

  22. haris April 17, 2012 at 5:46 am - Reply

    nice post i like this

  23. shrikant salunke June 4, 2012 at 1:15 pm - Reply

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

  24. Mayank June 5, 2012 at 11:26 pm - Reply

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

Leave A Comment

FREE QUOTE

Do you need help with services? Request a free quote to get in touch.