PHP Optimization Tips

By Deane Barker on October 16, 2007

40 Tips for optimizing your php Code: This is a seemingly great list of PHP optimization tips. However, there’s no context here or no discussion of why some of these techniques are supposedly faster.

If a method can be static, declare it static. Speed improvement is by a factor of 4. [….] echo is faster than print. […] Use echo’s multiple parameters instead of string concatenation. […] It’s better to use select statements than multi if, else if, statements.

I’ve seen some of these before, so I assume this is a roll-up of a lot of existing information.

I did like this part:

Not everything has to be OOP, often it is too much overhead, each method and object call consumes a lot of memory. […] Do not implement every data structure as a class, arrays are useful, too […] Don’t split methods too much, think, which code you will really re-use

I’ve always thought that if you had a single script that had to run crazy, crazy fast (say, an analytics tracking script), it would pay to code it without any thought to extension or abstraction. Use the base PHP functions, no database abstraction, all sorts of “echo” statements, etc. Always remember Joseph Scott’s database abstraction tests from last year — PEAR:DB added 80% execution time.

The bottom of this article provides a link to another post on PHP optimization that gets deep into tuning Apache to run PHP faster. Really interesting stuff.

What This Links To