Feb
25
More Reasons Why PHP Sucks
Filed Under Computers & Tech, Software Development on February 25, 2006 at 11:59 pm
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!
[…] no secret that I don’t like PHP. In fact, I dedicated an entire article to explaining just why I don’t like PHP last year. Recently I’ve found another reason to dislike PHP. The GD image manipulation libraries […]
hey, no wonder you hate PHP so much – you’re trying to use a nonexistent function call preg_repalce
^_^
oops … typo! Fixed now!
Cheers.
Instead you have to use strings which get interpolated once when being stored as a string
That’s a very good reason not to use the “quote” syntax in PHP, so that strings don’t get interpreted (or interpolated).
$ans = preg_replace(‘\$’, ‘\\$’, $ans)
The blog is being hosted with PHP..!!!!
LOL … yes it is, but I didn’t write WordPress, and if I had it wouldn’t have been written in PHP. An unfortunately large number of open source projects are written in PHP these days, so it’s hard to avoid using other people’s PHP.
PHP is not a language for everyone. I’ve seen some pretty crappy code in my time using it. A lot of people say it’s so easy to use (yet they code without a proper coding flow).
Anyone can code in PHP, however, not everyone can program with it.
That being said. Using it right, you can code just about anything with it. I’m a fan of OO-PHP. I don’t get why some people are so against it. I can code very clean efficient applications with it (some of which are highly scalable).
Noting a few examples as to why you think PHP sucks is well within your right. Remember that no longer is perfect. But I do find that PHP regularly meets my needs.
PS: I know quite a few apps which have been writing in PHP (facebook, WordPress to name a few)
There’s gotta be a reason for it. It might suck in certain areas (name a language which has no issues?) but overall it’s pretty damn good.
Hi Jero,
To each their own. It is possible to write good code in just about anything, even Assembly language, but that does not mean that some languages are better than others. I’ve yet to find a perfect one, granted, but the shortcomings I’ve pointed out here are a big deal for me. Text processing is important.
For me PHP will always be a poor Perl knock-off that tried for simplicity and ended up with complexity. But that’s just my view. Everyone should code in what ever they want.
Bart.