12 PHP optimization tips

Ilia Alshanetsky’s PHP performance talk given last week at the Zend conference is pretty useful as far as getting small tips for tweaking PHP code.

  1. If a method can be static, declare it static. Speed improvement is by a factor of 4.
  2. Avoid magic like __get, __set, __autoload
  3. require_once() is expensive
  4. Use full paths in includes and requires, less time spent on resolving the OS paths.
  5. If you need to find out the time when the script started executing, $_SERVER['REQUEST_TIME'] is preferred to time()
  6. See if you can use strncasecmp, strpbrk and stripos instead of regex
  7. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4
  8. If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments.
  9. Error suppression with @ is very slow.
  10. $row['id'] is 7 times faster than $row[id]
  11. Error messages are expensive
  12. Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time.

For templating, are you using Smarty? This is probably the fastest way to ensure the frequently viewed pages of the site are cached. Also, check out this list of high-availability and scalability presentations. There’s also a great article on references in PHP.

17 Comments »

  1. [...] Both this link and this link give PHP coders some tips in making faster and better PHP web applications. [...]

  2. [...] A blog has just posted some interesting PHP optimization tips taken from the Zend Performance PDF. [...]

  3. [...] Stop what your coding and read this…. and the slides [...]

  4. [...] Settimana ricca di avvenimenti questa, vediamo un po’ di tirarne fuori qualcosa di buono: 10 motivi per non passare a Windows Vista Uppity per Firefox (geniale) 10 Effective Ways to Get More Blog Subscribers La tecnologia ha amici in parlamento (era ora) Template professionali davvero belli Google Yahoo e MSN adottano lo standard SiteMap 12 PHP optimization tips firefox, Il meglio della settimana, PHP, sitemap, templates, vista, Windows. [...]

  5. [...] http://www.moskalyuk.com/blog/php-optimization-tips/1272 [...]

  6. [...] Fuente Original: http://www.moskalyuk.com/blog/php-optimization-tips/1272 [...]

  7. [...] 12 PHP optimization tips 12 tips para mejorar el rendimiento de nuestros scripts en PHP (tags: php tips) [...]

  8. [...] No pude evitarlo, son bastante buenos los Tips (Gracias Alma por la tradución ). [...]

  9. [...] pude evitarlo, son bastante buenos los Tips Compartelo Categoria : Notas Cortas Tags : Php, Programacion Post [...]

  10. 7 -> str_replace is way faster than preg_replace….! ereg_replace is slower, but since str_replace doesn’t use regular expression, you do the math…

    Also, watch those file system functions… File_exists for example is a performance killer.

  11. You have #7 backwards.

    Comment by philip — April 6th, 2007 at 1:05 pm
  12. [...] http://www.oreillynet.com/onlamp/blog/2006/04/digg_phps_scalability_and_perf.html http://www.moskalyuk.com/blog/php-optimization-tips/1272 http://ilia.ws/archives/12-PHP-Optimization-Tricks.html http://www.dublish.com/articles/10.html [...]

  13. Alright for tip number 10. You put $row[’id’], is that different than $row['id']? For me to type ´ I have to press ALT+0180. Or by chance is it $row[`id`] (tilda key)? Thanks.

    Comment by Ben — February 21st, 2008 at 8:25 am
  14. Nevermind…I tried it out and only $row['1'] worked.

    Comment by Ben — February 21st, 2008 at 8:53 am
  15. Ben, yeah, ` was a WordPress fancy quote.

    Comment by Alex — February 21st, 2008 at 12:23 pm
  16. [...] Use ip2long() and long2ip() to store IP addresses as integers instead of strings. [Citation] [...]

  17. [...] it comes to optimizing PHP for performance, there’s no end of resources available, and no end of conflicting opinions either. Everyone seems to have their own [...]

Leave a comment