Des made some great points and linked to some other great blog entries on all the things that are wrong with PHP in his recent blog entry I hated php back when it was cool and I found myself agreeing with them all but I also have another gripe with PHP so I figured I may as well share mine too while the topic is hot on Planet MiNDS>.

Firstly, I just want to own up and admit to being a huge PHP fan a few years ago. In those days I only did small web projects with a single developer, me! PHP was adequate for that and the learning curve was shallow and the development time low for those small projects. Since then I’ve done a number of much more substantial web projects and, initially I did these larger projects in PHP and just got more and more frustrated with it. Then, when I discovered Apache Struts I had one of those epiphanies and realised that PHP sucks!

So, Des & Co. have already expounded on many of the problems with PHP which make it hard to admin, hard to develop in and hard to keep secure but they didn’t hit on my little bug bear, PHP’s annoying regular expression support. Again, on paper PHP’s RE support looks great. You have a choice of your style of RE, either the preg or ereg functions, and preg claims to be Perl type syntax which IMO is the holy grail of RE support in a language. Where PHP falls over is that unlike JS or Perl or even Shell Script, it does not have regular expression literals. Instead you have to use strings which get interpolated once when being stored as a string and then a second time when it is interpreted as an RE, this means you have to double escape your REs and that is confusing as hell! What makes it even more confusing is that the replacements in preg_replace are ALSO double interpolated so they ALSO have to be double escaped.

To give you an example of how messy this gets here is the PHP code for replacing all $ characters with \$:

$ans = preg_replace("\\\$", "\\\\$", $ans);

Compare that to the JS code which is a lot clearer:

ans = ans.replace(/\$/, "\\$&");

I’m sure the PHP guys would say that having RE literals would ‘complicate’ PHP but I don’t buy it. The ‘simplicity’ of only having two data types, Scalars and Arrays results in making the use of REs needlessly complex and down right annoying!