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.
- If a method can be static, declare it static. Speed improvement is by a factor of 4.
- Avoid magic like __get, __set, __autoload
- require_once() is expensive
- Use full paths in includes and requires, less time spent on resolving the OS paths.
- If you need to find out the time when the script started executing, $_SERVER['REQUEST_TIME'] is preferred to time()
- See if you can use strncasecmp, strpbrk and stripos instead of regex
- str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4
- 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.
- Error suppression with @ is very slow.
- $row['id'] is 7 times faster than $row[id]
- Error messages are expensive
- 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.

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.
You have #7 backwards.
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.
Nevermind…I tried it out and only $row['1'] worked.
Ben, yeah, ` was a WordPress fancy quote.
Is it strange that all of these rules mine a good design?
Remember the saying ‘premature optimization is the root of all evil’…
I can use ten str_replace() instead of a regex, but is that code maintanable?
I can make function static, but if I do how can I test them?
I can avoid autoload() or spl_autoload stack, but it will mean add thousand of lines of require()..
$row[id]is a syntax error – too bad PHP isn’t treating it as one, but tries to think for the programmer.This is the first time I’ve ever seen Smarty suggested as a way to speed a site up. Template engines may have their place, but they rarely bring any speed benefits. Even with all of the compile checks off, Smarty is pretty slow (unless it has been dramatically changed in the last year or so).
If you’re wanting to cache content, use a proper caching lib.
Actually I believe that most of these hints are real minor when trying to tune a website. It’s good to try to keep things fast as you write them, but if you have an existing system it’s probably much cheaper to buy a second server to take the load instead of trying to replace all preg_replaces with str_replaces all over the code and win, let’s be generous, 1% over all performance…
These ‘rules’ might make a difference if you heavily rely on a single command, for example run preg_replace over 10.000 strings every time a user calls a special page. But if you have a single preg_replace on a page, who cares?
Same applies to almost all of these rules, with exception to 3, 4 (file system related) and 12 (plain stupid
). And also 7, but that’s because you’ll never want publically visible error messages on your production system.
Concatenation string is faster if you use coma (,) than dot (.) operator, but only if you print data to browser and not create a variable. For example:
$foo = ‘foo’;
echo $foo, ‘bar’;
is faster than
echo $foo. ‘bar’;
Not work if you want to create a variable:
$out = $foo, ‘bar’;
Praca,
Have you ever benched marked that “tip”.
Under my tests (in php5.2.6), the concatenation was always faster (though not by much)
I wrote that tip in one of the biggest php site on the Internet. I don’t remember site URL but the title was something like “40 tips to optimize your php scripts”.
http://progtuts.info/55/php-optimization-tips/