I should really buckle down and try writing a PHP app because, at the moment, I have an attitude problem. I know that IBM now officially loves it, and Tim O’Reilly’s been charting the upcurve in PHP book sales, and everyone’s saying that Oracle’s going to buy Zend. If you want your ears bent back, have a listen to Zend CEO Doron Gerstel; he’ll tell you that half the websites in the world are powered by PHP and that there are 2½ million developers and that the war is over and PHP won. So here’s my problem, 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. Everyone agrees on PHP’s upsides: it’s written for the web, it’s easy to deploy and get running, and it’s pretty fast. Those are important advantages. And I’m sure that it’s possible to write clean, comprehensible, maintainable, PHP; only apparently it’s real easy not to. But PHP has competition, most obviously Rails; and don’t write the Java EE crowd off, they’re not stupid at all and they’re trying to learn the lessons that PHP is trying to teach. So PHP has earned everyone’s respect by getting where it is, and Sun should reach out to it more than we have. But in the big picture, it feels vulnerable to me. [Wow, I regret not having comments. There’s been some first-rate discussion in email and on other blogs. On this occasion, I’m going to create a virtual comment section by posting the good ones here.] [There is a new, good pro-PHP rant from Harry Fuecks, and with that I’m going to stop adding to this discussion, unless somebody says something strikingly new. Thanks everyone! I’ve added a brief Table of Contents to try to bring some order to the chaos.]

These are lightly edited to remove remarks along the lines of “Been reading ongoing for years...”

Table of Contents · Forest Dean Feigher: a little PHP gallery.
Adam Kalsey: A hammer can be used to build a beautiful structure or a mis-shapen birdhouse.
Dominic Mitchell: there’s a lot to like in PHP. But it really needs to clean its act up.
Kenny Gatdula: Perl still has a few tricks up her sleeves.
Josh Hart: PHP can be elegant, can be used right.
Mike Papageorge: I think that it is possible to write agile PHP, in an unspaghetti-like fashion.
John Pile: the language needs a way to say “but seriously now”.
Simon Brocklehurst: Wordpress is a nice example of a great PHP app.
François Nonnenmacher: Warning, serious parody ahead.
Rafe Colburn: PHP is bad.
Jason R. Briggs: “Pretty fast”, in my opinion, is an understatement.
Tony Fisk: PHP may not be the most polished language out there, but it’s OK, rich, and workable.
John Lim: Is PHP the new perl?
Harry Fuecks: Tim Bray on PHP - with dozens of comments.
Henry Sivonen: In the 21st centure, just ignoring Unicode in the standard library and data types is pretty amazing in a bad way.
Tim Hatch: As long as you don't expect organizational miracles to come from its use, it's not bad.
Jeff Nolan: Using Python as a contrast...
Jonas Maurus: I’m sorry, but PHP sucks.
Steve Minutillo: PHP, XML, and Character Encodings: a tale of sadness, rage, and (data-)loss.
Faruk Ates: a lot of skilled PHP coders have become incredibly elitist and rude.
Aristotle Pagaltzis: PHP is... lousy. Just wretched. Why?.
Tommy Williams: the absolute beauty of PHP.
Harry Fuecks (again): A pro-PHP Rant; once again, with lots of comments.

Forest Dean Feighner · Please take a look at my little PHP gallery. It was written for a default install of Tiger and uses the mdfind and mdls utilities. It needs some minor cleaning up if your running PHP5. Perhaps you'll like SSG better than iPhoto in terms of Photocasting.

http://tw3k.net/SSG/

Best Regard
Forest Dean Feighner
http://tw3k.net

Adam Kalsey · If you want some support on your foray into PHP, please let me know. I’d love to lend a hand.

It’s easy to write bad code in PHP. Or in Perl. Or in C. Or Java. I think two things contribute to the perception that PHP code is crap. First of all, when you write spaghetti PHP, it still works just fine. So unless you’re a professional programmer or maintaining lots of code, you probably don’t care that the code is crap.

Secondly, since PHP is easy to learn, is ubiquitous, and there’s lots of PHP examples on the web, it’s easy for a non-pro to pick up PHP and start monkeying with it. They learn bad habits and then publish their code and teach others bad habits. Some of the most popular open source PHP applications ever are really poorly coded. Gallery and Wordpress come to mind.

A hammer can be used to build a beautiful structure or a mis-shapen birdhouse. And it’s easy to flatten your thumb. With so many amateur PHP developers out there, it’s not surprising there’s a lot of flat-thumb code in the wild.

Adam Kalsey
Blog: http://kalsey.com/
Work: http://tagyu.com/

Dominic Mitchell · I’ve just read your post on PHP... If you do get around to writing that book, it needs to be called “PHP for programmers” because that’s the market that’s been 100% unrepresented so far.

I’m in 100% agreement with you about the whole PHP thing. Every time I’ve looked at PHP, I’ve been unable to find out how they use placeholders in code that talks to the database. Yes, every database adaptor provides a quote function, but nobody uses it (consistently). And people wonder why PHP code is always full of security holes.

It’s a real shame, as I can see that there’s a lot to like in PHP. But it really needs to clean its act up to appeal to people coming from other programming languages, instead of just people coming from dreamweaver.

I actually heard David Heinemeier Hansson (sp?) talking about rails the other day. One of the key points he made is that it should be easy to do the right thing. That’s why Rails hits a sweet spot for me. But when you look at PHP, yes, it’s easy to code, but it’s not easy to do the right thing. That’s where it falls down.

-Dom

Kenny Gatdula · I read that you're looking at writing an app in PHP. I urge you to also consider trying Catalyst. http://dev.catalyst.perl.org/. It borrows from Rails, but, it’s backed by CPAN. If you want to try it out without bothering with the CPAN install, here’s an evaluation package. http://dev.catalyst.perl.org/wiki/CatInABox. I look forward to your post letting the world know that the war isn’t over just yet for PHP. Perl still has a few tricks up her sleeves.

Kenny (http://www.orbitalworks.com)

Josh Hart · I think you are right in that one of the problems of PHP is that it is so easy to produce really horrible code. And there is a huge amount of horrible PHP out there.

But PHP can be elegant, can be used right. I make no claim as to what is ‘best’ but I am convinced it can be an excellent choice for web applications. It may not be love but we are certainly very fond of PHP.

I have got an example... at least of an attempt to do things right. My company has developed a PHP based web application framework which we use internally for all sorts of projects. It’s very similar, in principal, to Rails, but we use PHP instead of Ruby. And while the code certainly isn’t perfect we try to do our best.

We’ve considered open sourcing the framework (we call it Stageblocks) but haven’t done that yet, mostly because we haven’t got round to it rather than any philosophical problem. There are already a lot of competing frameworks and Zend are apparently about to do their own which means it is unlikely it would gain any traction.

But it works great for us. We may open source it anyway but there is an admin overhead just in getting everything in place and we’re a very small (5 person) and young company. Bear with us.

Having said that, if you were interested in looking at the code (as an example of some, hopefully, elegant PHP) then I’d be delighted to send you a copy of a standard install subject to it being kept in reasonable confidence (or something, nothing formal).

In case you are remotely interested I’ve attached three very simple files pulled out of a current project by way of introduction. A sample model, view and a controller.

Finally, can I just reassure you that this really isn’t some kind of sales message. I’m not ‘looking’ for anything here, just responding to your blog which I thoroughly enjoy reading ;-)

Of course, having said all that we think, ultimately, Ruby may threaten PHP if it continues to grow as rapidly as it is currently is. We’re all dabbling with Ruby (especially Rails) at the moment and it is an enticing option.

All the best

Josh Hart
Made Media Ltd
www.mademedia.co.uk

Mike Papageorge · I posted a sort-of-retort-more-like-agreement post on my blog: http://www.fiftyfoureleven.com/weblog/web-development/programming-and-scripts/on-php.

Cheers - Mike Papageorge

John Pile · Well - it hasn’t been proven that users *can’t* write clean, comprehensible, maintainable PHP - but it also hasn’t been proven that anyone can.

The smartest guy I know, and I know some scary-smart people, spent a significant amount of time building a test-jig and class library for developers to build up system integration tests in PHP. (The reasons for the language selection were mundane and/or pointy-haired).

In the end it was a scary failure, even though it was only deployed to a small group of relatively proficient developers, working in an extremely narrow use case, testing just one application.

At the very lowest level, there is no way to enforce discipline in checking return codes - there is no “set -e” equivalent. So, you can spend a lot of time writing wrappers that do things like call an operation and then check that the operation didn’t complain...but you can’t prevent the users from circumventing the checks.

Loose typing is post-modern and fun, but the language needs a way to say “but seriously now”.

There are all sorts of other little unfixable details which time has graciously allowed me to forget.

Put differently, you can’t put more safety into a language than it ships with. I seriously doubt that any developer can build a production-quality app using PHP. For the record, I don’t consider even big-name web sites to be “production-quality”, because they fall back on a squishy meat-bag to click “refresh” if something goes wrong.

I wish we had used that same developer time to grout tile rather than build on PHP.

-jP

PS: Oracle has made at least one monumentally stupid purchase in the last year, so I don’t think their alleged interest in Zend means anything...

Simon Brocklehurst · Manual trackback: see Tim Bray on PHP.

François Nonnenmacher · Satirical, funny: On Java.

Rafe Colburn · Entitled PHP is bad, and there are interesting comments too.

Jason R Briggs · He write on php performance, saying my “pretty fast” characterization is understating it.

Tony Fisk · It may seem obvious to a veteran like yourself, but I have always had a mental block went it came to understanding how languages like Perl or Python are used to write web pages. HTML I can understand, and I have written Python scripts for stand alone use, but I always felt that a piece of the puzzle was missing.

This piece was first provided to me by PHP. Embedding the code in html and telling the webserver to use a preprocessor gives me a simple process to follow and understand.

So, I appreciate PHP for the leg-up it gave me, if nothing else. Now, having spent a large portion of the last year trying to de-obfuscate a large amount of legacy PHP code (and learn to program for the web!), I definitely appreciate that it’s *real* easy not to write something clean, comprehensible and maintainable!!

However, I don’t think the fault was wholly with PHP! I think bad code can be written in any language (just as good code can be written with gotos, not that I’d want to). I recently was given some Ruby code to tweak, and encountered a situation where fully populated variables were appearing out the blue with no indication of how they were defined! I spent a day or two trying to work out where the magic happened. It turned out that the original author was using an obscure Ruby feature rather than a named array to pass variables via the symbol table. Hyperspatial travel is clever, perhaps, but far from obvious!

PHP may not be the most polished language out there, but it’s OK, rich, and workable. It feels much like C, with a few extra bells and whistles (foreach, typelessness, and some OO support that’s not great, but is supposedly better than Perl. There’s even a unit testing package available: SimpleTest http://www.lastcraft.com/simple_test.php). So clean, well structured code is certainly possible.

Where people might fall in a hole is in understanding how a PHP script runs: a bit like a cascading state machine that finally stabilises into a web page showing one bit of html or another, depending on the initial conditions. Trying to allow for different results in the same script can lead to a lot of decision making and lots of “if” statements if you don’t understand subroutines and the power of function references.

In covering the field of scripting languages for the web, you might like to consider a dark horse: Spyce (http://spyce.sourceforge.net/), which provides a PHP like environment for Python. Given my own learning experiences, and given that Python is a much cleaner language than PHP, I can see the benefits! I haven’t used Spyce much, but it seems to work OK, and has a cute logo.

Regards, Tony Fisk
http://randomised.blogspot.com

John Lim · He asks Is PHP the new perl? saying “The abuse is what makes it popular.”

Harry Fuecks & friends · Over on the SitePoint blogs, Harry starts a conversation with Tim Bray on PHP; there’s tons of follow-up commentary.

Henry Sivonen · I noticed that neither you post nor the comments mentioned PHP’s lack of Unicode support. Strings are byte strings. There is no Unicode string type. Also, functions for working with the byte strings as UTF-8 strings are missing from the repertoire of standard built-in functions. (There are multibyte string extension functions, but they are not guaranteed to be available and, hence, cannot be used.)

In the 21st centure, just ignoring Unicode in the standard library and data types is pretty amazing in a bad way. (Disclaimer: My experience is with PHP4, but people who have used PHP5 say nothing has changed in this area, which is, in a way, even more disturbing.)

I once wanted to add a feature to the in-house CMS of fiMUG (Finnish Macintosh User Group; http://fimug.fi/) and discovered that I needed conversion between UTF-8 and Windows-1252. It was not part of the platform. I had to write the code myself as an app developer! That’s code which in Java, Python or Perl is part of the standard library. When I was done porting a converter (http://hsivonen.iki.fi/php-utf8/) from Mozilla’s code base (the first place where I could find one), I no longer had the time to implement the feature I needed the converter for.

As for code quality, I later found PHP examples that dealt with UTF-8 and none of them used the obvious bitwise operations that any programmer familiar with low-level stuff would use with UTF-8.

Also, I think the entire concept of mixing program code with snippets of HTML source text is inherently tag soupish and spaghetti-inducing. It only appears to work because HTML browsers are forgiving. (JSP is not any better. It combines the tag soupness of PHP with the heaviness of Java.)

Best regards,

Henri Sivonen
http://hsivonen.iki.fi/

Tim Hatch · He’s pretty well in the “anti” camp: On Bashing PHP.

Jeff Nolan · A fairly straight Python-vs-PHP take: PHP.

Jonas Maurus · Another un-friend of PHP: I’m sorry, but PHP sucks. How about somebody sends me some good pro-PHP rants?

Steve Minutillo · Someone sent me a pointer to Steve Minutello’s howl of anguish from last June: PHP, XML, and Character Encodings: a tale of sadness, rage, and (data-)loss.

Faruk Ates · It’s true that most PHP you will encounter is ugly spaghetti-code that does nothing more than what it’s meant to do and leave whoever reads it in disgust. So why is it so popular?

The people telling you that PHP is (one of) the most popular web programming language are probably fanatics, as there is an equally great amount of ASP.NET and Java, J2EE and JSP programmers out there. The latter groups are simply less vocal about it, and they are less scattered and widespread.

PHP is like HTML in that it is extremely easy to get a head start in. You don’t need to know much about programming to start using PHP for some basic scripts and functionality, just like you don’t really need to know anything about semantic markup to start (ab)using HTML. For these same reasons, it has become so popular.

There are many little script examples for PHP because so many people want and need them. PHP largely caters to a crowd that doesn’t care much about programming, but rather about having some server-side power and functionality they can use in their site.

ASP.NET and Java programmers (going by examples that I know personally) are more recluse in their presence online. People programming in these languages are more likely to be actual programmers who want to do programming, and thus take it seriously. This doesn’t necessarily equate to *good* programmers, or quality code for that matter, but these aren’t as often people who just want a little scripting added to their site.

PHP, with its embedded nature, makes such things incredibly easy. These people then post their code as examples to others, and so it spreads. We see it with the millions of horrible HTML tutorials on the Web, we see it with PHP examples. It’s much less so with ASP.NET or Java because those languages are a lot harder to work with and thus don’t appeal to the “quick and dirty” crowd — a crowd that is unbelievably large.

But all this doesn’t make PHP a bad language by itself. Like Javascript, it’s not the language that is to blame for the horrible uses, but the people using it in evil ways.

I have been creating Content Management Systems in PHP and SQL (MySQL/PostgreSQL) for over 5 years straight, now, and my code has changed from the typical PHP-crud to very clean, reusable and efficient code. I’ve learned from good examples such as vBulletin’s PHP code (that, as of version 3.x, could be considered a great example), but good examples may have clean and efficient code, they are too complex to be understood by that very big “quick and dirty” crowd that makes up a large portion of the PHP user base.

What PHP’s challenge is right now is its slow improvement, and the fact that a lot of skilled PHP coders have become incredibly elitist and rude, making it unappealing for newcomers and beginners to learn proper coding methods. In contrast, the Ruby on Rails crowd is growing rapidly and its user base is much friendlier and helpful, making RoR a good candidate for knocking PHP off its (self-imposed) throne position.

In the end, we may all be better off with that outcome, because there’s no sign whatsoever that PHP will ever become a predominantly clean language, not even counting all their UTF-8 issues.

Faruk Ates
Web Kaizen Specialist
www.kurafire.net

Aristotle Pagaltzis · Another heartfelt anti-PHP rantk, with more technical specifics: On PHP.

Tommy Williams: the absolute beauty of PHP.

Harry Fuecks & friends, Redux · His A pro-PHP rant is smart and focuses in on the execution model.


author · Dad
colophon · rights
picture of the day
February 17, 2006
· Technology (90 fragments)
· · Software (82 more)
· · Web (396 more)

By .

The opinions expressed here
are my own, and no other party
necessarily agrees with them.

A full disclosure of my
professional interests is
on the author page.

I’m on Mastodon!