DeviLib - library about web, IT, programming and web design

20 PHP optimization tips - make it faster

PHP main pageOptimizing PHP is of a great importance, though it has not much impact in a small script, however the bigger the script is becoming some shortcomings can cause much inconvenience later. Here are several facts (although some of them could be disputed about) that may help you in choosing the the right way to achieve the best results.

  1. echo is faster than print, despite of that both are constructors, print is returning boolean result, echo returns nothing.

  2. printf is much more slower than echo or print, it must handle additional parameters

  3. avoid as much as possible magic methods like __get, __set, __autoload

  4. when searching string in another string strstr() is faster than preg functions, however if you want to know if that string exists use strpos().

  5. ++$i works faster than $i++. This is PHP unique behavior.

  6. echo ‘Hello world’ works faster than echo “Hello world”, although speed difference is very tiny.

  7. $arr[‘id’] works much more faster than $arr[id]

  8. if you need to print a bigger piece of text without PHP variables just exit PHP (?>) and enter it when you need it again (<?php).

  9. ctype functions are faster than regular expressions. More info at http://php.net/ctype

  10. errors suppression with ‘@’ is useful, but makes script load slower.

  11. use full OS paths, /home/user/public_html/images/ instead of just images/

  12. don’t use variables if they will be used just 1 time.

  13. if you want to assign variable to one of the array indexes, don’t do $var=$arr[‘foo’], better choice would be use references. $var=&$arr[‘foo’], now you just creating a reference to $arr[‘foo’] instead of duplicating it’s value.

  14. use $_SERVER[’REQUEST_TIME’] instead of every time calling time() function. For example declare constant at the config file: define(“TIME”, $_SERVER[’REQUEST_TIME’]) and use TIME when you want to know timestamp.

  15. isset($arr[‘foo‘]) works faster than in_array(‘foo’, $arr)

  16. require_once() is useful, but works slower than common require()

  17. never use unwanted functions in loop header, for($i=0; $i<count($foo); $i++) - will count $foo on every loop step

  18. don’t use database queries in loops. Every major database system has enough possibilities to handle out that situation

  19. mysql_fetch_array() will return an array with both index types, mysql_fetch_assoc() will not return numerical indexed array, mysql_fetch_row() will return just numerical indexed array

  20. You must avoid several writing in one file. Better choice is to collect all data and with one writing session update file.

To conclude, it not always possible to follow these ways of optimizing the PHP. The main point of optimizing is that if you include as much optimizing tips in your script, the shorter the execution time of it will be and vice verse – the longer the script with only minimal optimization tips will prolong its execution time.

Related posts:

  1. 10 Responses to “20 PHP optimization tips - make it faster”

  2. By php on Nov 1, 2007 | Reply

    Good one
    http://www.w3answers.com

  3. By ziogas on Nov 1, 2007 | Reply

    Thanks :)

  4. By Tim on Nov 7, 2007 | Reply

    I’d like to add a rule:
    21. if( condition ) foo(); else boo(); works faster than ( condition )?foo():boo();
    as Ilia Alshanetsky says.

    Tim
    http://gtd-tools.com

  5. By SylverStyle on Nov 19, 2007 | Reply

    here are some more PHP optimization tips
    http://ecastr.com/phoenix/blog/details/advanced_php_optimization_tips_chapter/
    http://ecastr.com/phoenix/blog/details/advanced_php_optimization_tips_chapter_2/

  6. By Chiron on Feb 7, 2008 | Reply

    I liked these tips, but I would disagree with one of them. I would use a variable, even if it’s only used once. The reason is that you never know when you’re going to make a change that would reuse that variable. The insignificant saving of time you might get would be swamped by the time you’d spend rewriting your script. Also, the presence of hard-coded values would tend to discourage you from thinking of reusing a value as a variable, since it would present a problem.

    Just my take on it…

  7. By ziogas on Feb 7, 2008 | Reply

    In that point of view you are quite right, thinking about time spending in the future, you should declare a variables even if it’s used only once. But it still less efficient than write directly variable value although that “efficient” is very small and in 99.99% that difference just imperceptible

  8. By rudy gee on Feb 7, 2008 | Reply

    duh. same post different site x eleventy billion.

  9. By ziogas on Feb 8, 2008 | Reply

    It’s a collection of tips, do you expect that here (or anywhere else) are still super unique tips that will improve code quality twice? I don’t think so :D web is just too large that we can avoid partial duplication

  10. By Ari on Apr 26, 2008 | Reply

    isset($arr[’foo’]) is not the same as in_array(’foo’, $arr)

    in_array — Checks if a value exists in an array
    isset($arr[’foo’]) Checks if a key exists in an array.

  11. By marcus on Aug 6, 2008 | Reply

    How many times will we see this exact same blog entry on stumble? If you want hits to your site, write something original!

Post a Comment