Those of you who have been around, probably know that PHP is one of those languages that people just love to hate. For any number of reasons. Yes, it has a vast global namespace full of redundant functions, and yes, their names don’t make sense half of the time, and of course, the order by which you pass the arguments seems to depend on how drunk the developer of that particular function was when he wrote it.
That’s all true, and I get annoyed by certain aspects of the language, too. But I feel as though PHP has been bashed enough. It’s flawed, chaotic and is a prime example of a language design style I call “Design by after-thought”. But I’m going to try to write a post in defense of PHP nonetheless. I will be responding to 2 arguments that most anti-PHP rants seem to share: Language design and PHP’s alleged legacy. However, I will not be arguing that PHP has produced great things, point at yahoo! or facebook. That wouldn’t be fair. I’m talking about a more general, abstract and indeed historic legacy…
Design isn’t everything
There is a well know blog post out there entitled “PHP: A fractal of bad design”. Sure, the author makes a couple of good points here and there. Other points of his are no longer true, or are overstatements, but I still found his post a nice read. One thing still bugged me, though. While it is true that PHP wasn’t really designed as such, that doesn’t mean nothing good can come from it. Some argue that C++ is a horrible language: designed by committee, too vast, and so complex that it conspiracy theorists took notice. Does that mean that everything written in C++ is bad?
What’s more: I’ve had to work with a language that was designed, with a clear goal in mind, and every aspect of that language was created with that primary objective in mind. The entire language served a purpouse, and the best way to get there was agreed upon beforehand. That sounds like a recipe for a pretty solid design, no?
Well, no: if your objective or purpouse is a bad one, then your design will be pretty horrid, too. Before you comment saying how much you disagree with me on this, you should know that the language I’m talking about here is… COBOL. If this brings back nightmares of days past: I’m sorry for mentioning this monster. For those of you who don’t know what COBOL is, here’s a quote of the famous computer scientist Edsger W Dijkstra on the language:
The use of COBOL cripples the mind; its teaching should, therefore, be regarded as a criminal offense
Sounds like the worst language in history… perhaps because it was created by soldiers.
Never blame the tool, if it is being used by a bigger tool
This is another one of those arguments that have been dismissed by anti-PHP blogs. I still maintain, though, that you can’t classify a language as no good junk, just because some lunatic has written crappy code in that language. Sure, it is more common to see crappy PHP code than it is seeing crappy C code, for example.
In a way, one could argue that this is the fault of PHP: not forcing its users to write better code. But how far can you take that? Java goes to the other extreme, and annoys the crap out of me. Whenever I compile some Java I’ve hastily written, I feel patronized by javac, or whatever compiler I happen to use. Java compilers behave as though they’re smarter than the people who wrote them: “You forgot something… [smirk] if you do this, then you should know that an IOException could be thrown, silly”. How I loathe artificial intelligence at times like that.
Of course, PHP should be less lenient, or at least have its runtime set to strict standards by default. But really, PHP isn’t the only language that could do better at this.
Common horrible practices like mixing database connectivity and queries in with HTML will send every decent coder off in a boiling rage. And rightfully so. That’s a terrible thing to do, and PHP allows you to do just that. That’s one of the things that shouldn’t be allowed.
Just like accessing memory you don’t own and writing to it is a horrible thing to do, but you can do it in C and C++. Apart from Java fanatics, I haven’t met anybody that called C a badly designed language for allowing you to access out-of-bounds memory.
You could also write a C program that renders a GUI and connects to a database, too, and you could write the code that does both of these tasks in a single source file. But it’s highly unlikely you’ll ever find code like this out in the wild. This is partly because C developers tend to be more skilled than those writing PHP, and of course because such code would be impossible to maintain.
Still, is it fair to blame the language, or should we blame the monsters writing bad code? I say we blame the monsters.
Mixing output and DB connectivity is possible in PHP, but anyone with even half a brain cell should be able to work out that this is bad practice. Just like it is possible for me to drink a bottle of scotch while playing twister on a rooftop, I know not to do that.
PHP lets you do bad things, and that won’t change any time soon, but let’s adopt the attitude of the C standard: If you do that, it’s your problem and you should’ve known better.
It has produced some great things
This is the other argument that has the power to set most PHP-dislikers off in a boiling rage. Sure, I know: Facebook has changed the entire runtime and even created a new language, so I’m not pointing at Facebook. Yes, Yahoo! was written in PHP, but again: the runtime is heavily modified and they use hundreds of custom plugins.
What I mean when I say it produced some great things is that PHP opened up webdevelopment. It’s easy to learn, and it allowes a lot of people to play and tinker with programming, and get that feeling of accomplishment and excitement that drew me (and hopefully you) to learning more about programming.
If you’ve never taken the time to listen to Rasmus Lerdorf talking about how, why and when he first started work on PHP, and you’re an anti-PHP person: bear with me on this. Steve Jobs wasn’t the innovator people seem to think he is. He was the best marketeer silicone valley has ever seen. He managed to turn computers from something geeky amateurs loved into a lifestyle product. He turned PC’s from an ugly beige block you hid into actual desirable products. No more, no less. A similar case can be made for PHP: Lerdorf did not set out to create a new, well designed language. He understood that the future of the web lied in accessibility. Everybody should be capable to put his content out there, not just people with a substantial knowledge of low-level programming languages like C and all of the protocols involved. Remember: PHP’s original name was PHP Toolkit, where PHP was an acronym that stood for Personal HomePage, it’s goal was to open up dynamic websites to less technical people.
Say what you want, but this idea was indeed a game-changer. PHP enabled amateurs to create dynamic web pages, with minimal effort and at low cost. Sure, now we have other languages that can be embedded in markup, like ASP or JSP. But those technologies simply didn’t exist when PHP made its debut.
I’m not claiming the design of PHP influenced other languages (much), but the idea was picked up on. As an idea, allowing people to easily create dynamic web pages is a brilliant one. It’s an idea that was, and still is, being copied all over the place. Some PHP-haters are actually using languages that adopted this concept. Looking at it from that angle, it has to be said: PHP did produce some great things.
It’s not as much where you start, nor is it the road you take, but it’s where you end up that matters. I started off with an old mac, and a booklet called “Microsoft Excel Function Reference”. From it, I learned the basic logic constructs: if’s and else’s.
I dabbled with visual basic, and learned about loops, functions (or sub‘s). Then, I took some courses in PHP, and I’m still mighty glad I did. Here’s why, and here’s how I think of PHP:
PHP helps you to find out what you want to do
I learned PHP, at a time where I thought I wanted to become a web designer, with a bit more knowledge of the web, so I could work together with the backend developers. After a while, I realized that front-end development was a bit to tedious to my liking. And my having the artistic eye of a dead wombat didn’t help either.
About that time, PHP5 was introduced, and the ZendEngine2 promised to be a lot better at OOP. I had been putting OO off for quite some time, because there simply was no need for it. At least not for the way I used PHP. That is one of the nice things about languages like PHP or Python: they allow you to sneak up on complex concepts like OO, You can still get that sense of achievement and personal satisfaction even if you haven’t yet learned to think in Objects. Java doesn’t give you that. But then, Java doesn’t give you an awful lot, except a bleeding ulcer.
I wanted to be a web-designer, and was able to sample what that meant, through PHP. I then wanted to be a back-end developer, and PHP provided me with ample opportunities to sample that. I liked some aspects of it, and hated others. I then went through a phase of exploring other languages that would improve my chances of becoming a software architect of sorts. I learnt C++, dreaded Java, and in my spare time played with Scheme and Haskell.
This was also to distract myself from the fact that I had to write a lot of COBOL at work. To all those who call PHP a crime against humanity, you don’t know what you’re talking about.
Eventually, I returned to PHP. you may find this a surprising decision of mine. I have worked with languages that are a lot better. I love C, for example. Recently, I picked up a hitchhiker, a computer-science student. We chatted a bit, and I told him I was a developer. When I told him I work with PHP, he expressed his dismay of the language, and asked me why I didn’t share his opinion. I couldn’t really explain why I still really like PHP, despite all of its faults.I guess writing PHP is just effortless, to me. It’s the first language I learnt thoroughly, and it’s the language that got me my first job in IT, so I owe a lot to it. Of course, I took my current job because I didn’t have to write Java or Cobol anymore, but I can write plain old C, instead.
Of all the languages I’ve tried over the years, that is the only language that still amazes me: being as small as it is, and knowing it has been with us for over 40 years, it is still a gorgeously designed language, and still useful. PHP might not be as nicely designed, in the right hands, it can be useful, still. Look at seasick Steve: some hubcaps, a broken gardening tool and a spachelor can turn into musical instruments in his hands.