I Will Never Understand the Appeal Of PHP

December 09, 2006

Recently I was reading about a plugin for embedding adsense code automatically in blog posts, downloaded the code to see what it did, and found it to be php. Although I've never used php in any formal sense I have read enough to understand the "language". How PHP can be so popular is beyond me; the whole sense of encouraging all of your html, code, bindings, javascript, even sql (as in the below example) into a single file is a nightmare.

My first experience with JSP in its first year generated a similar reaction, the ability to embed html, javascript, java, and some odd xml in the same source file seemed convenient for about one day. The next day I couldn't make heads or tails out of what I had written the day before. I quickly came up with using a java class to hold all of the logic and only embedded the bare minimum in the jsp itself. Eventually the project shifted to the now common (but still fairly new at that time) full MVC approach. Splitting up the application into logical sections or layers proved the only way to manage and maintain development of a serious web application (we had something like 80-100 pages). Keeping the jsp file as clean as possible also made it easier to change the html design (which changed fairly rapidly over the initial 6 month development). This was also before tag libraries, so you still had to have some minimal java snippets, but at least it was fairly localized.

In PHP there is no alternative to embedding everything together; although you can certainly include other files, there are no real namespaces, so you can't really separate concerns without artificial complexity. Everything is interpreted (which I think is its main appeal) so changes appear directly without any need to compile or redeploy (anyone using traditional J2EE application servers like Weblogic will groan at that word, for me with Jetty it's about 2 seconds). It is appealing - for a while. Everyone I know who works with PHP admits that their application code gets uglier and less appealing over time even if they work hard at trying to keep order. Bugs and flakiness occur with greater frequency as portions of the code get less and less readable, especially as programmers turn over and are less familiar with the nest of language elements in each file. This can happen in any language but the rate of deterioration in PHP is way faster than the expected lifespan of the application.

In my last job the main storefront was written in PHP two years earlier, and listening to the programmer stuck maintaining the beast (not the original coder), the constant problems the storefront had, and the shear inability to make serious feature changes left no doubt it needed to be thrown out.

If there is anything that I have learned over the years is that you can make something run out of almost any technology. However I have also learned that just because something is easy and quick, it doesn't mean its going to stay that way. Very often an application spends far more time in maintenance than in initial development, so being able to jump in and understand some code you didn't write is really important and far more common than starting from scratch. With PHP (and ordinary JSP and ASP as well) I rarely see code that can be jumped into; most of the time you just want to throw it away and start over. With a proper separation of the application's layers/parts/components you can more easily understand the code and make changes without creating more bugs. Unfortunately with PHP (among others) it's usually beyond the developers to write easily maintainable applications as the language simply doesn't allow it.

Of course code in any language can be poorly written; I've seen my share of abysmal java code, objective-C, C++, C and even assembler. It seems to me that using a language well and avoiding its stupidities isn't always that easy (and many programmers have little discipline to learn how) but with PHP it seems the programmer benefits are far outweighed by its drawbacks. I bet the ultimate PHP coder can do a careful job of creating good code, but the problem is that the average coder is highly unlikely to due to the language itself.

function tracker()
  global $table_prefix, $wpdb;
  $table_name = $table_prefix . "mightyadsense";
  <script language="JavaScript">
  var iFr;
  function log() { 
  var loca=document.location+"";
  if (window.status.indexOf('go to') == 0) 
  bug = new Image(); 
  if(loca.indexOf('?') > -1)
    bug.src = loca + '&site=' + loca + '&target=' + window.status.substring(6) + '';
    bug.src = loca + '?site=' + loca + '&target=' + window.status.substring(6) + '';
  var loca=document.location+"";
  var elements; 
  elements = document.getElementsByTagName("iframe"); 
    for (var i = 0; i < elements.length; i++) { 
    if(elements[i].src.indexOf('googlesyndication.com') > -1) 
    elements[i].onfocus = log;
  if (isset($_GET['site']) && isset($_GET['target'])){
    $mdate=date ("Y-m-d h:i:s");
    if($wpdb->get_var("show tables like '$table_name'") != $table_name) {
      //create table
      $sql="CREATE TABLE `".$table_name."` (
      `mdate` DATETIME NOT NULL ,
      `ip` VARCHAR( 15 ) NOT NULL ,
      `site` VARCHAR( 255 ) NOT NULL ,
      `target` VARCHAR( 255 ) NOT NULL ,
      PRIMARY KEY ( `mdate` )
      require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
      //insert click
      $insert = "INSERT INTO ".$table_name.
                " (mdate,ip,site,target) ".
                "VALUES ('".$mdate."','".$cip."','".$site."','".$target."')";
            $results = $wpdb->query( $insert );