The Web's moved on, so why does PHP still exist? 3rd February 2010
I'll say right off the bat that I'm not a PHP fan. It's ugly and its only good point is its seeming ease to learn. I think this is a fallacy, as I'll try to explain. If you're not a programmer I'll try and make as much sense as I can.
Facebook has launched a new project, HipHop for PHP. Born out of a mammoth hacking session, it attempts to rewrite the core source code of PHP to make it speedier and more robust. A noble, but to my mind pointless task, as that's not the problem that PHP really suffers from. In my opinion, it's just outdated. I'm also a little disconcerted with the opening gambit from the project's Facebook page. Here it is in full; I'll break it down bit-by-bit to try and explain why I think it's wrong.
As a programming language, PHP is simple. Simple to learn, simple to write, simple to read, and simple to debug. We are able to get new engineers ramped up at Facebook a lot faster with PHP than with other languages, which allows us to innovate faster.
I should state off the bat that the developers behind this project are obviously more experienced, and most probably more intelligent - and better coders - than me (and I say that without a sneer and without irony), but that doesn't make them right about PHP. This is why (the points are taken from the quote above, and relate to PHP as a scripting language):
PHP is "simple to learn"
No. Basic, Python and Ruby are simple to learn because they're simple to read. (We'll get onto that in a bit.) The more readable a language is, the easier it is to understand and learn. The more consistent the syntax, the quicker the repetition takes hold and familiarity sets in. PHP does not have consistent syntax. (See below.)
PHP is "simple to write"
No. The statement "x = 1 + 2" which could be written the same in Python and Ruby (and similarly, with a small caveat in Basic) is written in PHP as "$x = 1 + 2". OK, not that different, but longer to type because of the dollar sign, which needs two keystrokes, breaking the flow of typing.
Attaching one string to another (where a string is a piece of text) is done in well-considered languages by using the + sign, because that makes sense. Some, like Basic use the & sign, which also makes sense. But in PHP, you use a full stop. That's down to lazy language design, in my opinion. That doesn't make it easy to learn or easy to write, because you have to have that unintuitive rule in place in your mind when you type.
PHP is "simple to read"
God no. PHP is traditionally used in web pages, and like ASP - easier to read than PHP but just as outdated - where the code is mingled and mangled together with the HTML. Yes there are frameworks that separate the two out, but many template frameworks - things like WordPress for example - still use heavy blocks of PHP. And when most (but not all) people talk about PHP, they're talking about it "raw", with nothing built on top.
Even a standard block of PHP with no other elements is difficult to read, because of the inconsistent syntax and unnecessary extra characters. When you're glancing through a script trying to get its gist, dollar signs can look like Ss, commas can look like full stops, and curly brackets can be confused with non curly brackets (the two types of bracket having two separate meanings). That last point is true of C and its descendants (C++, C#, ActionScript and JavaScript), so it's not a deal-breaker.
PHP has a nice facility for injecting variables (pieces of information held in memory) right into strings, without having to join them together using the full stop. But you can only do that if you use the right quote marks. Why? Python can do it either way, as (I think) can Ruby. C# and Visual Basic use a function to do it, but in all these cases the rules are consistent.
PHP is "simple to debug"
That depends on what you think of as "debugging", and your definition of "easy". To some, that could mean loading and reloading a page, making tiny tweaks to code until they see the desired effect. In others, like C#.NET and VB.NET, it's done by "stepping through" each line of code at a time, so you can see the exact state of play before an error or unexpected action occurs. In other languages or frameworks you can test your code in a console (a terminal window where you type commands and get quite responses).
PHP pretty much just deals with the first case, and that's only if the host or computer you're using has turned error reporting on (which isn't done by default). If not, you don't see any error messages, just (in some cases) a blank white page.
I'm trying to compare like for like as much as I can here, so as not to be unfair to PHP. It's not a framework, in the same way that Ruby on Rails is. But as a language, Ruby is fair superior (Rails is the framework that is built ontop of Ruby, to make it work on the Web).
I think PHP still exists because it's used by Facebook and WordPress and a few others that haven't yet got round to updating their codebase. I know that's a little trite and if you're a pure PHP guy I'm sorry to insult your religion - please shout at me in the box below - but I really think it's time we put it out to pasture. I don't think there's a place for it in the modern Web, even with the frameworks that exist to hide its sloppy workings and ill-considered design. It goes against the things we praise other developers for: keeping code separate, using object orientation (if you're unfamiliar, it's something PHP hasn't which almost all other languages have).
I care about the Web. And I don't want to make it harder for people to develop for and play with it either. I'm not interested in pulling the ladder up after me so that all the people with the intelligence and desire - but not the knowhow - to develop, have to come to me to develop for them. I get really excited when people tell me they're learning Django or even Rails, because I think "that's great; there's another one who's doing it right". I want the Web to be filled with really well-built things, not stuff "knocked up" in 5 minutes 'cos it was quicker. I get why that's done, I just can't understand why it's not worth taking the time to do it better.
But maybe I'm just elitist. Or a zealot. Or a code snob. Or an unsung hero to the masses? Almost definitely not the last one, but I'll let you decide. Feel free to tear me a new one in your comments.
One comment
David North
February 3 2010, 10:14am
I have to agree. The only reason I see PHP as such a force to be reckoned with is the large user-base.
The reason it's still being used even though effectively outdated? Laziness (maybe a little harshly worded) - there is no direct upgrade path so changing language requires a fair bit of retooling.
I'm still amazed at what is produced with PHP but just wonder what those clever guys could come up with if they didn't have one hand tied behind their back.
Leave your comments