Best PHP Practices for a Fast Site

Discussion in 'PHP' started by Chris Munch, Aug 23, 2010.

  1. #1
    I'll admit I'm not too techie when it comes to PHP, but I've had a bunch of developers working on my sites to improve their loading times. We've had some awesome results so far, and we are still trying to squeeze out extra milliseconds.

    PHP Code is only part of it, but I'm well aware that bad PHP code can slow down the site.

    In your view what are the best practices for maintaining a fast site when using PHP?
     
    Chris Munch, Aug 23, 2010 IP
  2. HuggyEssex

    HuggyEssex Member

    Messages:
    297
    Likes Received:
    4
    Best Answers:
    2
    Trophy Points:
    45
    #2
    Making sure that double quotes are used when needed.
     
    HuggyEssex, Aug 23, 2010 IP
  3. CLDPFY

    CLDPFY Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    When it comes to php, one thing that is key is using an oppcode cache. Personally, I use APC but I've used others in the past. That way the code is ready to run and doesn't need to be compiled each load. That one bit alone will have a dramatic impact upon speed.

    Assuming the site is backed by a database, then optimizing the queries that are used is vital. This is everything to ensuring that the tables are properly indexed and that the indexes are actually being used to denormalizing the data to allow the queries to be simplified to using integer based indexes for the vast majority of queries.

    Finally caching, caching caching. One level of caching is the mysql query cache which will have various levels of affects based upon the application. Sometimes the query cache can hurt performance (lots of inserts) but sometimes is provides a really painless way to speed things up (small write load, huge select load). Things like memcached to stuff prebuilt pages into so that they can simply be pulled out and displayed are another highly effective way to speed things up.
     
    CLDPFY, Aug 23, 2010 IP
  4. andymoo

    andymoo Peon

    Messages:
    169
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You're wasting your time on speeding up PHP.

    Speed up the client side first not the server side.

    Trimming 100th of a second on ttfb with your PHP sript isn't going to save you anywhere near as much time as compressing all the images, adding the right headers and so on.

    Steve Souders books are worth a read.

    He goes into the maths much better but seriously, server side tweaks play such a little effect compared to client side ones.
     
    andymoo, Aug 24, 2010 IP
  5. Chris Munch

    Chris Munch Peon

    Messages:
    235
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This is similar to the caching that plugins like W3TC do for wordpress right?
     
    Chris Munch, Aug 25, 2010 IP
  6. CLDPFY

    CLDPFY Peon

    Messages:
    76
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Since I don't run a big wordpress farm (my current stuff is based on drupal) I don't have direct experience with this plugin. However, I did check it out and their feature list is exactly the kind of thing I'm talking about.

    For my drupal sites, I make use of http://drupal.org/project/memcache to move the cached objects out of the DB and into memory. Now that the wordpress mu stuff is baked into the standard wp, I may go back to using it again and move my blog sites out of lifetype.

    My sites are still not as fast as I want them to be, mostly because they are data heavy and we are constantly processing new data which puts a strain on the db but getting the cache out of the db and into RAM certainly helped. Now I just need to find a reasonably efficient method of priming the cache with thousands of objects.
     
    CLDPFY, Aug 25, 2010 IP
  7. Chris Munch

    Chris Munch Peon

    Messages:
    235
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Gotcha, we've actually used memcache already, I just forgot what the hell it did. Thanks for reminding me :)
     
    Chris Munch, Aug 25, 2010 IP