-
Get a monthly update on best practices for delivering successful software.
It was with great amusement that I read PHP Sucks, But It Doesn't Matter, a recent post from Jeff Atwood's Coding Horror. Here's an excerpt (emphasis his):
Some of the largest sites on the internet -- sites you probably interact with on a daily basis -- are written in PHP. If PHP sucks so profoundly, why is it powering so much of the internet?
The only conclusion I can draw is that building a compelling application is far more important than choice of language. While PHP wouldn't be my choice, and if pressed, I might argue that it should never be the choice for any rational human being sitting in front of a computer, I can't argue with the results.
With all my recent work on the Pathfinder blog migration, I've been getting to to know WordPress - and, by extension, PHP - pretty well. Now that the experience is (mostly) over, I've got a few observations about WordPress:
Let me elaborate.
WordPress is full of arcane syntax. Some of its template tags take a number of named arguments. Some take a single, querystring-esque argument that encapsulates a number of additional arguments. Tags with parallel names do not employ parallel APIs. And despite WordPress's reputation as the blogging platform for customization-happy, code-writing geeks, too many of its methods are geared toward folks who just want what everybody else is having.
Many methods are designed merely to spit out a collection of HTML links, but there's often little or no way to customize those links within the method invocation. Sometimes you can pass in a keyword to toggle between a finite number of markup patterns. Sometimes you even can pass in arguments containing specific HTML in which to wrap each returned link. But there's no way to, say, apply a class to each link. If you want that level of customization, you've got to edit the PHP methods directly, which sets you up for heartache when you want to upgrade.
For many common tasks, there's a built-in method that gets you most of the way there. But often it won't get you all the way there. And there are still several tasks that completely lack a built-in method. You can easily build a list of author-specific RSS feeds with wp_list_authors, but there's no parallel method for building a list of tag-specific RSS feeds. For that, you'd probably need to clone and customize wp_tag_cloud.
Is there a corollary between the beauty of a language and its usefulness and power? I wonder because PHP looks so ugly. All of those endless underscores. All of those functions with names like a_function or the_method. And for a templating language, why choose to separate your server-side code from the client-side template? It's just hard on the eyes - much harder than the <% %> of JSP and ASP.
Still, as I've said before, I'm overjoyed to have migrated to WordPress. WordPress template tags might not be pretty, but they get me most of the way there - and they're endlessly hackable. By supporting themability, a plug-in architecture and the flexibility of PHP itself, WordPress offers more freedom than any other blogging platform.
As an example, let's take my desire to add a custom headline to our single-author archive pages (here's mine). There was no built-in method that I could find to simply output the author name at the top of the page. Any such method had to be invoked within "The Loop" - the main page method that iterates through posts. Within a few minutes of digging through documentation, I realized that my page could iterate through that loop twice as long as I "rewound" the loop after the first pass. Voila, a bit of custom code that, although clunky, get the job done:
<h1>
<?php while (have_posts()) : the_post(); ?>
<?php if ($wp_query->current_post == 0) { ?>
<div>
Author Archive:
<?php the_author(); ?>
</div>
<?php } ?>
<?php endwhile; ?>
<?php rewind_posts(); ?>
</h1>
Related posts:
“All of those endless underscores. All of those functions with names like a_function or the_method.”
While that is the convention for PHP builtins, you can name your user-defined methods/functions using camelCase if you want.
“It’s just hard on the eyes – much harder than the of JSP and ASP.”
If you’re talking about the use of <?php, you can shorten it with <? and <?= (which is equivalent to <?php echo). You can even (until PHP 6, I think) use ASP-style <%. However, most PHP apps will not use short tags because it’s a PHP initialization setting to allow it and not all hosts may have it enabled. This is also why many PHP apps are written for PHP 4; there are still plenty of hosts that haven’t upgraded to 5 even though it’s been out for a long time.
Check out http://us2.php.net/manual/en/language.basic-syntax.php for some of the ways you can make the template-level PHP prettier.
Comment by Karen, Tuesday, May 27, 2008 @ 4:31 pm
After reading the linked article and this one, one can draw the conclusion that any programming language sucks. I think the reason PHP gets so much heat is due to the way the majority of PHP developers write their PHP application (see: wordpress). PHP is the HTML 4.01 of the server-side world, it is very tolerant to incorrect standards while still producing the required results, but if used correctly is every bit as viable as its brethren.
The key is seperation of logic and markup, similar to developing the front-end of a javascript/ajax app. If you litter your HTML with ‘onmouseover’ or ‘href=”javascript:”‘ your going to make maintenance and modifications a pain in the a**. Same with PHP, if you little your HTML with inline your going to make maintenance and modification a pain. Good coding standards makes everyones lives easier, PHP’s only sin is being too lenient in that department (like javascript).
Comment by T.J., Wednesday, May 28, 2008 @ 1:05 pm
Edit to my above comment, PHP code removed.
Same with PHP, if you litter your HTML with inline [php] get_something() [endphp] your going to make maintenance and modification a pain.
Comment by T.J., Wednesday, May 28, 2008 @ 1:10 pm
Wordpress is completely procedural in implementation, and it shows in its mess of a code. If you want to judge PHP quality done by professional developers, take a look at the code of one the major frameworks (Zend Framework, CakePHP, Symfony). I’m sure you’ll think much better of PHP afterwards.
Comment by Eran Galperin, Wednesday, May 28, 2008 @ 6:01 pm
Well I do think WordPress serves its role as a good blogging platform. I started my own blog a couple days ago and must say I’m quite impressed with the ease of use and clean look and feel you get from it. That said, I haven’t come about ANY open source application written in PHP that looks even decent.
As Eran said, Cake, Symfony and the like are changing this, but it’s past dooms its present if you ask me. Take phpMyAdmin for instance: a very good database administration application, widely used, with one hell of a mess in its codebase.
I do think though that PHP is a valid web application development platform, it just isn’t used correctly, because as others said, it’s just too lenient. If you take for instance C# or Java, they mostly FORCE you into using object orientation to some extent, and have a community built around standards and best practices. Hell I haven’t even come over a PHP Coding Standards document yet.
Comment by Alex Reis, Thursday, May 29, 2008 @ 12:41 pm
It’s been pointed out to me that as a passionate advocate and programmer of JavaScript, I probably shouldn’t jump on the bandwagon so much when people bash programming platforms that are traditionally considered kids’ stuff. Thanks to all for the tips on quality PHP projects to check out for examples of PHP code done right!
Comment by Brian Dillard, Monday, June 2, 2008 @ 11:24 am
The underscores in PHP are to do with the naming convention used by developer(s) and nothing to do with language itself.
Some goods points made here about ugliness of WordPress tags. I don’t like the look of them indeed.
Comment by Jason Grant, Thursday, June 12, 2008 @ 10:28 am
So I started up a new site, http://www.highlandparkmonitor.com, and put it on a WP recommended host. And Jesus H. Friggin Christ I cannot believe how much trouble I am having getting the WordPress theme to support simple, basic functions that worked fine when I was hosted at WordPress.com. Even installing a Sitemeter has proved impossible: Sitemeter’s own instructions are out of date and reflect none of the dashboard terms.
Comment by Steven Hart, Saturday, September 5, 2009 @ 10:19 am