Yet Another Stupid Perl Versus PHP Rant

Espe­cial­ly because of all the work I’ve been doing in Dru­pal, I’ve switched most of my devel­op­ment efforts away from Perl solu­tions and start­ed work­ing most­ly with PHP. The «Perl ver­sus PHP» flame­wars on the Inter­net are all over the place and usu­al­ly full of non­sense. I real­ly don’t want to add to that. In fact, I’m pret­ty pleased with PHP. The lan­guage has come a long way since it’s days as PHP/FI. I’ve come to appre­ci­ate its vari­able scop­ing scheme, which caused me fits when I first start­ed port­ing code from Perl to PHP. If noth­ing else, it keeps me from typ­ing «my» every time I want to declare a vari­able. Seri­ous­ly: today default scope being glob­al seems like a hor­ri­ble design deci­sion. But I digress.

Here’s today’s PHP gripe: if PHP is sup­posed to be the lan­guage for build­ing Web­sites, where are the func­tions to auto­mat­i­cal­ly make HTML form (and oth­er) ele­ments? I’m spoiled by Perl I guess, but the fact that I can say I’m spoiled by Perl should be a slap in the face of all those out there look­ing down their igno­rant PHP noses at Perl Web devel­op­ers. When in Perl I want to cre­ate a selec­tion box (pull-down menu), I take an array and an asso­cia­tive array and plug it in to one of the CGI.pm methods:

$q->popup_menu(
  -name     => 'tour',
  -values   => \@tourlist,
  -labels   => \%tours  );

It’s mean­ing­less with­out know­ing what data is in the %tours hash and the @tourlist array, but just bear with me.

With PHP, there’s no such library of handy HTML-gen­er­at­ing meth­ods. I’ve looked. I don’t under­stand why no one thinks it’s impor­tant. I know that HTML code is pret­ty easy, but the clos­est equiv­a­lent to the above in PHP goes like this

<select id='tour'>
<?php
  foreach ( $tours as $tourid => $label ) {
?>
  <option value='<?php echo $tourid; ?>'>
    <?php echo $label; ?>
  </option>
<?php
  }
?>
</select>

…all right, that’s not the end of the world, but it takes two actu­al skillsets and some thought to con­struct, not to men­tion the code is more dif­fi­cult to main­tain. If you want to update your code so that the HTML fol­lows some new cod­ing stan­dard, using the mod­ule you can change the options for gen­er­at­ing the HTML, and sud­den­ly see your changes in effect every­where. Even using auto­mat­ic search and replace you can’t do that with raw HTML code unless you are real­ly, real­ly good about writ­ing con­sis­tent code.

It’s just me grip­ing. I still like PHP.

Leave a Reply