Coding in the Rain

When it rains in the Boston Area, PHP development increases at the StripedCow headquarters. When the sun’s out and the birds are singing, it’s easy to get distracted. We’ve had so much rain and cold weather over the past couple weeks that I was able to hack a bunch of PHP code. I was working pretty hard until I got distracted by bad PHP code examples.

As a software consultant, I often come across poorly written code. For the most part this is code that was thrown together quickly or was modified by several authors who didn’t understand the original code. That’s all fine, I can accept that. Some things are written inefficiently because the coder doesn’t know the language. That’s a little harder to swallow, but I can understand how that can happen.

The thing that bothers me? Poorly written code samples that are posted on a website to show how to perform a specific task. For example, this function to pad a number with zeros:

function zerofill($num,$zerofill) {
  while (strlen($num)<$zerofill) {
    $num = "0".$num;
  return $num;
echo zerofill(2, 5);

The output of this code would be “00002”. Basically, this is duplicating the functionality of the built-in str_pad() call: str_pad(2, 5, '0', STR_PAD_LEFT);

Both of these work fine, the zerofill() function would be slower and has less functionality than the str_pad() call, but they’ll both operate for the case that was demonstrated. I wouldn’t mind coming across this in code that I was maintaining, I’d just assume that someone unknowingly did things the hard way.

The problem is that this code was put on a webpage as a demonstration of the author’s PHP expertise and an example that beginning PHP programmers might copy into their own code.

To make matters worse, the webpage goes on to show how to remove the zeros with another piece of PHP:

function zeroremove($value)  {
  return preg_replace("/(\.\d+?)0+$/", "$1", $value)*1;
echo zeroremove('00002');

The output of this code would be “2”.

I’m a big fan of programming with the assumption that the next person that looks at the code is just out of college and may not be an expert programmer (or know regular expressions). Luckily the preg_replace() call was wrapped in a function that will make the code obvious.

There are several ways of converting a string to a number and using a regular expression will certainly work. But it’s inefficient and shows a lack of PHP knowledge and (worse) shows the author PHP doesn’t explicitly support type definitions. Looking back at the return value, the output of the preg_replace() call is multiplied by 1. This is the author’s attempt to cast a string value to a numeric value.

As a short cut, the regular expression call could have been removed and the line simplified to: return $value*1; (multiplying the string by one will automatically cast the variable to a number and return the proper value). When using preg_replace(), it’s unnecessary to cast the value and (better yet) the built-in PHP function intval() was created for this purpose.

Another thing that’s annoying, I had to reformat the code so that the two snippets match in style. The author of the web article couldn’t even manage to keep the same spacing for two small pieces of code.

But that’s a rant for another rainy day in the Boston Area.

[Lee Stewart]October 05, 2010