I’m quite surprised to see this great stir in the blogosphere caused by Tim Bray and his essay On PHP. I mean, what’s the deal anyway? We all know that PHP is a lousy programming language. So instead on focusing on the big picture and trying to list as many of its faults as I can remember, I’ll stay on one specific problem, which is really a pain for me.
I’ve been programming in PHP for the past five years (I’ve started using it in the beginning of 2001, during my Computer Science studies). PHP is (was?) both my love and hate. My biggest complain about it, and probably about all other programming languages used for web development, is that there is no decent way of producing HTML code from your application. No matter how hard you try, you’ll always end up having spaghetti either in your HTML or in PHP code (or even worse, in both places). Sure, separating business from presentation logic is a common practice which helps the cause (apart from the fact that most PHP folks mix HTML with PHP whenever and wherever they can). Problem is, that it’s not good enough.
It would be all much simpler if you didn’t need to put any logic into your presentation code. The moment you introduce loops or conditionals – bam – you either get unreadable code or spaghetti HTML with way too much whitespace (or without any whitespace at all) and improper indentation. Not to mention that I am probably not the only one sick of typing all those nasty angle brackets all the time. But what about all that specialized templating engines? Frankly, they are not any better. Besides the fact that some of them require a certain overhead (like defining the variables you wish to use in your templates – Smarty, anyone?), they all still carry the same burden – they either produce spaghetti HTML or have spaghetti templates.
Actually, Rails, being my current weapon of choice, is no better in this area. My .rhtml templates are a complete mess. I strive to have my HTML as pretty as it can be, because (maybe strangely) I find it easier to debug if I have a nice HTML output to deal with, instead of a nice .rhtml template. Too bad that I feel utter disgust when I look at those templates. And just as Smarty is no better for PHP, Liquid and similar projects are no better for Rails. However, there is still hope, with projects like Markaby (which, unfortunately, has its own issues, check the comments).
Just a small digression, to sum all this up. Rails has solved one other, big inconvenience, namely writing your own SQL for each and every interaction with the database. Thanks to ORM provided by ActiveRecord, you don’t have to write SQL any more (at least in most of the cases; the 80/20 rule). This is one of the basic reasons that Rails is so much better than PHP. Markaby tries to do the same to HTML, what ActiveRecord did to SQL. No more those nasty < /> characters! We’ll see how (and where) it goes.