June 19, 2013

You use PHP because you don't know better.

Wherein I state my opinions about PHP so I need never discuss the matter again

My typical response when asked to defend a given offhand, derogatory comment I’ve just made about PHP is to cite the most excellent PHP is a fractal of bad design. But, spurred, by the numerous comments hating on me for hating on PHP on this Portugese translation of my PHP vs Python vs Ruby vs Clojure piece (which, to be fair, is emphatically biased), I’m going to go ahead and let the hate out now, as one big rant, so that I have something to refer to in the future (even though I bet I’ll find it embarrassing and childish then).

Disclaimer

Most times I write on here, I try to add value to the world. What follows is not one of those times – less “constructive exploration”, more “bitchy rant”. I disclaim that here because, hey, why ruin a good rant by riddling it with qualifiers.

If you’re currently a PHP developer, I mean no personal disrespect, but before you scroll down the to the comments to join the fray, I do sincerely and honestly encourage you to learn another way to make web pages. Worst case, you come back to PHP with an expanded perspective, because that’s how learning works. Leave your arguments about market and economic and employment viability at the door. I promise – nay, guarantee – you will be better off for it.

Also, if you still prefer PHP having learned and seriously used one of the incumbents, please do leave a comment below. After all, your very existence is a counterargument, and what could be more constructive than that?

Ok, disclaimer over.

PHP, as a programming language, is the worst language among all the languages I know how to program in. I have written and deployed code in PHP, C, Python, Java, Clojure, Coffeescript and Javascript, and dabbled in many others. PHP is, by a wide margin, the least consistent, most error-prone, worst-designed language of any I know.

PHP is designed to be an HTML templating language, and should have stayed one. It was written by an admitted amateur (at the time! I’m sure Rasmus could do better these days if he started from scratch, and I mean no, uh, specific disrespect), and it shows.

The (weak) type system is complete rubbish, and the situation would be much improved if every variable was a string, which is how it treats them in most cases anyhow. Note to future language designers: if your language can’t overload “+” for string concatenation, have a good hard think about why that might be instead of just using something else.

The perl-esque $var syntax would be acceptable, if unnecessary, but for the nauseating and frankly dangerous $this->$var-style metaprogramming it enables.

Object-orientation features are completely bolted-on, even if they’ve become generally capable since PHP4. Ditto for exception handling.

The @ error-suppression operator is just a huge red flag.

The one-pass compilation system is just plain developer-hostile. In what other language does array("a"=>1, "b"=>2)[0] cause an error? And don’t get me started on the array() pseudo-function, or the conflation of arrays and hash-maps (my god, are they even hashed? It wouldn’t even surprise me if not).

The management of imports/includes is just plain unhygenic. Namespaces improves the situation, but only presuming whatever you’re just mashing all up into your code remembered to use one.

But of course, if there’s anything more tired than listing off the things that are wrong with PHP, it’s the arguments people make for it.

“Oh, you just aren’t up to date with all the cool new features that PHP 5.x brings to the table.” Bullshit. The only reasons anyone starts a new project in PHP these days are:

“You can write great code in any language.” Yeah, and Leonardo could’ve produced great art with human shit, if he cared to, but he knew better.

“But Facebook/Etsy/Wordpress/Mailchimp use PHP.” Those were all written before anyone even know about Ruby on Rails – let’s say 2005 or so – and more or less by people that didn’t know better. But you can’t blame them for that, because there didn’t exist the varied and mature alternatives that exist now.

“PHP has way better performance than Rails/Django/X.” Yeah, as long as you don’t get fancy and start calling functions all over the place. Take a look at this: performance of a sampling of popular frameworks. If you sort by latency, you can see a lone entry for “PHP” in the top half, at around 104.5 ms. Then, another “PHP” at ~500ms – this one uses an ORM instead of raw queries in the benchmark. Then, you start to see PHP frameworks, Cake, Symfony, Silex etc., clustered near the bottom.

You can argue that PHP is easy, and you won’t hear a peep from me. PHP still exists because, to this day, I can spend $5 on a cheap shared host, upload a PHP file, and see a website. I have no doubt whatsoever that the continued existence of PHP is thanks to the widespread adoption of Apache and mod_php.

But I’ve never heard an argument that PHP is a good, or even acceptable language, except from a) those with a deep investment in PHP, and b) those who have never used anything else.

PHP in 2013 is a step between HTML and a “real” platform – an easy little starter language that you can get up and running before you take the time to learn something better. Go learn to make websites in Python, or Ruby, or whatever (I suggest Clojure!), and then come back and tell me you still prefer PHP.

Edit: Apparently this was on HN while I was asleep, but was modded off the front page due to some policy against flamebait rants, a criteria which this post fits like a glove. The comments are still there and still good, so here’s a permalink.

Also, while I’m here, I’d like to reiterate that the above is a total rant, obviously, and that you shouldn’t take it as, you know, a real article with nuance and rationality. Feel free to discuss below, though.

A special message

This is where the affiliate links live, but hear me out! I use these two services every day, and I wouldn't recommend them if I wasn't satisfied.

DigitalOcean - Purveyors of fine (and inexpensive) virtual servers. I use DigitalOcean to host Address Bin and a few others; it's my go-to host. Use this referral link for a $10 credit.

AirBnb - I've been living in AirBnbs for over a year now, and plan to for many more. If you've ever wanted to try them out, you can get a $25 discount from this referral link.