On PHP - My Thoughts

Published in Programming and Scripts on Saturday, February 18th, 2006

So a couple of weeks ago I was in London at the "Future of Web Apps" conference, where I had the pleasure of bending David Heinemeier Hansson's ear about his view that there is an aspect of the devil in PHP that makes it easy to code poorly. that PHP is the devil (whispering in your ear) and that Ruby is the Angel

Now I see that Tim Bray has written On PHP, where he states:

...based on my limited experience with PHP (deploying a couple of free apps to do this and that, and debugging a site for a non-technical friend here and there): all the PHP code I’ve seen in that experience has been messy, unmaintainable crap. Spaghetti SQL wrapped in spaghetti PHP wrapped in spaghetti HTML, replicated in slightly-varying form in dozens of places.

Now, these are two very distinct points of view (but perhaps the same conclusions). David had worked for 5 years with PHP before moving into Rails, and no one will doubt that he knows his stuff. Tim knows his stuff as well, but admittedly has very little experience with PHP. Anyone else see the hole here?

Everyone's invited!

The keys to the PHP mansion are not hard to get. The entry level is low, so low, in fact, that almost any hack can write some code and make an app out of PHP, be it an actual Web based application or a simple mySQL driven website.

Herein lies the main problem: people with little or no education in the realm of programming are programming! And these are oftentimes people who don't understand what POST is, or GET, or even a header for that matter.

The fact is, PHP makes it easy to make a dynamic website, just as Dreamweaver makes it easy to write HTML, and we're all familiar with the mess that program spits out.

So of course people are going to write spaghetti code, they are going to repeat themselves, and the result is going to be a pain the in ass to maintain and improve. I should know, I have no formal programming education, and when I look back on the first few scripts I wrote I cringe - well, now I laugh - at how green I was (I also wonder how I had the knackers to charge for the work!).

What to do? Well, I think that it is possible to write agile PHP, in an unspaghetti-like fashion, that will result in an approximation of elegant code. I say approximation because I am certain that there are some things that Ruby, for example, will do better than PHP.

Anyways, by following some basic concepts, like using the Model-View-Controller pattern at least in spirit with all of your code. This, and more good advice, will help to eliminate spaghettiness right from the get go.

End of the rant

Yeah, so my point was that PHP can approximate elegance. There is a place between or perhaps equal to the antithesis of David's devil and Tim's position that its all pasta, where PHP can be beautiful and easy to manage and agile to develop.

Preemptive strike

<cheeky>Those of you who want to comment that Rails allows you to write beautiful code right out of the box, please re-read the post as you missed the point.</cheeky>

Updated: One last word

Tim Bray concludes On PHP by stating But in the big picture, it feels vulnerable to me.

I have a feeling that isn't true. So long as there is a huge user base of these (respectfully) PHP-noobies AND people who are "doing it right", so to speak, PHP isn't very vulnerable to these other languages.

Comments and Feedback

Isn't the whole principle behind this argument that elegant programming is based on knowledge and the more you know about good programming practices, the better you can perform in your language of choice be it PHP or Ruby? And besides, can't using a framework like Ruby on Rails be a bit of overkill for some simpler dynamic projects?

Hey William... WRT your first question, that is pretty much what I am saying. People seem to come across a lot of spaghetti code scipts in PHP, and my point was that is a result of the low entry level, not the language itself.

It is a tool, after all, what you do with that tool is another thing.

WRT your second question, I would imagine so, just like anything, you have to choose the right tool for the job.

There's always Cake or one of the other php rapid development tools which can do the same thing as rails, that is, force some level of elegance in your code.

And I say 'force' as lightly as possible. Of course, if you try hard enough, you can write spaghetti code in any language.

Yeah, I'm a new person to PHP and I agree with this article. You're correct, I churn out some messy horrible rubbish! But the great thing is, I can get better. I think PHP is the way to get better, quickly. In the meantime, lets hope the amount of damage I do is minimal.

The only things PHP is guilty of, is being ubiquitous and having a very low learning curve. I would think the fact that halfwits can piece together a junky web app, and the fact that almost any web hosting offers php standard, is a testament to its greatness. Its no coincidence that IBM is backing PHP and the Zend team.

Thinking of the low entry level of PHP resulting in spaghetti code, I would only note that the low entry level of page layout in general using a program like DreamWeaver doesn't help either. Having gotten tired off writing HTML, PHP, JavaScript, CSS, etc. by hand in a text editor, I took a shot at using DreamWeaver to design a web interface for a MySQL database. The resulting code was astonishingly redundant and incredibly unwieldy, not to mention hard to update and maintain. This wasn't code written by a novice, but code written by a machine simulating a novice very badly. A year later, I'm still trying to find the time to rip out all of the DreamWeaver code and start again from scratch. (It works, but it's slow...)

Wow, lots of comments...

Jesse, you could, but that is one of the knocks against frameworks, is that a person never really gets to know how to program properly. But if it gets the job done, maybe it doesn't even matter.

Ian, I'm writing something for next week that maybe you will find interesting. Sort of a link dump for new-ish people to PHP, pointing to some useful articles...

Chris, I agree!

Seth, good luck! I use DW for coding, but I hand code in the code view. I haven't found anything else as stable that has the FTP built-in...

No matter what people say, I think Java is best. Building webapps is so easy; there are so many good frameworks such as Struts, WebWork and Spring to build upon. Ant does a great job building and deployment all your tiered code; from your JSPs, servlets / beans layer, DAO, business logic, it handles it all.

Is it slow? Yes. But hardware and better software can curb those limitations. I'll take a webapp that I can extend, maintain and scale.

Hmm, Mike - I'm curious, did you talk to DHH about how he started Rails?

I ask because DHH started writing Rails (or so I've heard) in PHP and came to the conclusion that it simply couldn't do what he was looking to do.

To my experience today, this is still the case. While I agree there's no reason for PHP to be messy or pasta-filled crap -- it simply can't compete to Ruby's elegance. Yes, I do know PHP supports OO (and I use it whenever I program). Yes, I do know a lot of the things that PHP can do, but at the end of the day Ruby is simply a more graceful language.

I see posts like these so often it hurts. I was a PHP officianado since version 3 or so, and I loved it. I thought it was the best thing since sliced bread. But the difference lies in that I used Rails and used it for a lengthy time. I think after working with Ruby and working with PHP, there's simply no contest. All arguments of PHP approximating anything close to Ruby seem utterly rediculous and just... wrong.

But then again, just my point of view. I think there's a large separation between those defending PHP's elegance against Ruby, and those who have used both PHP and Ruby and see no reason to even try and compare the two.

Hey Wally, to each their own, I suppose. But keep in mind here that I'm not saying that PHP is the best tool for everything, by any means. I'm sure that Java is better for some things then for others - though I wouldn't know as I haven't had a crack at Java :D

Kyle, I vaguely remember him mentioning that about starting in PHP. To be honest, the thing I remember the most was him looking across the table with a gleam in his eye, shaking his head saying "you will" after I said that I don't give in to the devil (of PHP).

Anyways, I'm not sure you got this post. My point wasn't that PHP is more elegant than Ruby, or that it could be as elegant (read the part "End of the Rant") or that it can do what Ruby does. In fact it has nothing to do with Ruby vs. PHP!

The point is that some people dig on PHP from an unfair angle (and have been doing so for years, long before Rails), and what Tim presented in his post was just that: based on my limited experience with PHP (deploying a couple of free apps to do this and that, and debugging a site for a non-technical friend here and there).

He should do what he says he is going to do: I should really buckle down and try writing a PHP app because, at the moment, I have an attitude problem.

Why? Because PHP is a tool, and it's how you use it that counts. Some concepts transcend languages. They can be applied in PHP. They can be applied in Java. And they can they can be applied Ruby. Just don't dig on PHP because anyone can use it almost anywhere with very little up front knowledge. Simple as that.

Mike, what I was referring to in your post were the numerous statements leading on towards things like (notice the word "equal"):

Yeah, so my point was that PHP can approximate elegance. There is a place between or perhaps equal to David's idea (the angel) and Tim's position (its all pasta) where PHP can be beautiful and easy to manage and agile to develop.

What I'm trying to point out is that PHP has the ability to not be ugly. But based on the underlying limitations of the codebase, it cannot be elegant (comparative to Ruby, for instance).

And, if you set for the post to not be about Ruby vs PHP, you should try and not compare the two several times throughout the post. I've fallen into the trap many times, because at the end of the day you really are comparing two languages whether you intend to or not.

PHP isn't as OOP as I'd like, and once you start with PHP 4 you'll never get used to objects in this scripting language...

Kyle, don't be such a fanboy ;-) I compared Ruby to PHP once and Ruby came out the winner: I say approximation [to elegance] because I am certain that there are some things that Ruby, for example, will do better than PHP. And this in fact strikes a lot like your statement: it cannot be elegant (comparative to Ruby, for instance).

Did you miss that pink box at the end of the post? I'll say it again: my point was not to match the two together. Get over it, already.

For what its worth, the angel means doing things the right way, not giving in to the 'dark side' of bad habits that are so easy to do in PHP.

"PHP can be beautiful and easy to manage and agile to develop."

Mike, do you actually have the data to back that up?

We've all been saying for years that it isn't that elegant PHP is impossible, it just isn't encouraged, but at some point you start lose faith that you'll ever see any.


Way to make the back of my brain buzz all day thinking about this.

PHP, in and of itself - or within the boundaries of it's limitations - can be "beautiful and easy to manage and agile to develop". Granted, it can also be spaghetti and very hard to follow.

I'm aware that other languages have some features that PHP simply doesn't offer, but a comparison wasn't what I was after.

So, where Kyle states that PHP has the ability to not be ugly, he is comparing PHP to Ruby. I think I'm calling his not ugly beautiful PHP.

As far as data? I can only really go by my own experience I suppose. Ongoing projects tend to increase in elegance as I become a more proficient programmer, and old projects simply become more and more embarassing :D

[Elegant PHP] just isn't encouraged

Exactly. I wonder what the Newsvine code looks like, or other big PHP powered projects?

Why doesn't PHP have a standard MVC framework? Java has one, Ruby has one, Perl has one... why doesn't PHP?

Well first PHP has a much larger user base in the web development sense then Ruby, Perl, or Java.

And second only Ruby has a standard MVC framework. PHP like Perl and Java have many options.

Some PHP MVC frameworks: http://www.phpwact.org/php/mvc_frameworks

Zend does agree with that there should be one that is standard: http://www.zend.com/collaboration/qanda

Joshua, ya beat me to it. Thanks...

I'm not a PHP advocate, however you can write poor, unmaintainable code in any language. You should be pointing the finger at the author and the community for perpetuating the current cycle that PHP is in.



I've been programming for more years than I wish to admit and I find PHP simply great! I don't care if someone writes spaghetti code or the most elegant oop there is -- the point is php works, it's free, it's a good development environment, and people can learn to program with it.

I remember when "acceptable" programming was only done on mainframes and the rest of the world was at their mercy as to what we wanted and how we wanted it. After all, "they" knew better than us.

Then came along a little thing called the desktop computer -- of course we called them micro-computers back then, but the point was that programming, regardless of approved methods, was open to civilians. Now anyone with an Apple][ could pound out code and generate as many errors as they wanted -- bugs ruled the day!

What did this do for the rest of the world? Well... for one thing the spreadsheet was invented. Word processing became something for the masses and eventually the Internet came into being.

So, don't worry about how well people program or if they know the "proper" way, just realize that no progress is made through the efforts of those who know better, but rather through the efforts of those who don't know any better and do it anyway.

Long live spaghetti code and those who write it.


Home » Blog » Web Development » Programming and Scripts

Check out the blog categories for older content

The latest from my personal website,
Mike Papageorge.com

SiteUptime Web Site Monitoring Service

Sitepoint's web devlopment books have helped me out on many occasions both for finding a quick solution to a problem but also to level out my knowlegde in weaker areas (JavaScript, I'm looking at you!). I am recommending the following titles from my bookshelf:

The Principles Of Successful Freelancing

I started freelancing by diving in head first and getting on with it. Many years and a lot of experience later I was still able to take away some gems from this book, and there are plenty I wish I had thought of beforehand. If you are new to freelancing and have a lot of questions (or maybe don't know what questions to ask!) do yourself a favor and at least check out the sample chapters.

The Art & Science Of JavaScript

The author line-up for this book says it all. 7 excellent developers show you how to get your JavaScript coding up to speed with 7 chapters of great theory, code and examples. Metaprogramming with JavaScript (chapter 5 from Dan Webb) really helped me iron out some things I was missing about JavaScript. That said each chapter really helped me to develop my JavaScript skills beyond simple Ajax calls and html insertion with libs like JQuery.

The PHP Anthology: 101 Essential Tips, Tricks & Hacks

Like the other books listed here, this provides a great reference for the PHP developer looking to have the right answers from the right people at their fingertips. I tend to pull this off the shelf when I need to delve into new territory and usually find a workable solution to keep development moving. This only needs to happen once and you recoup the price of the book in time saved from having to develop the solution or find the right pattern for getting the job done..