Inline Values: The Ternary Operator

So, you have a text field, and you’re using an associative array to fill the values based on table data from your RDBMS, right?

You’re filling the values inline, something like:

value=”<?=$row['name']?>”

Which is great, so long as you’re sure the row will exist. If the row doesn’t, you get an E_NOTICE telling you there’s an undefined index. You could turn off error reporting, and poof, it works, or you could do it right.

Turning off error reporting to mask sloppy coding practices is like stopping doctor visits once you’ve been diagnosed with cancer. You’re still sick, you’re just not hearing about it any more. And just like a disease will continue to have adverse affects whether you admit it or not, your server’s error log will fill up with undefined index and undeclared variable warnings and notices. Will this cause a long term server problem? Maybe, but I doubt it.

More important, filling up the server log with garbage makes you less able to diagnose real problems and fix them, because you have to sift through millions of lines of stupid notices and warnings to get to find a “real” error.

Let’s say you’re pulling the row like this, and assigning the array:

$result = mysql_query(”select * from users where id=1″);
$user = mysql_fetch_assoc($result);

If there is no record, you have a problem. Granted, most programmers I’ve seen are too sloppy and lazy to care about such a problem, but the problem still exists regardless of how you feel about it.

In a situation like the above (which is not the best way to access a DB, IMHO, use classes and good architecture), you can short circuit the issue by declaring the empty array first and then using a decision block to init the data if there is something to init.

$row = array();
$result = mysql_query(”select * from users where id = 1″);
if (mysql_num_rows($result)):
  $row = mysql_fetch_assoc($result);
endif;

This step alone does not solve your problem, it lays the foundation by which your problem can be solved. Now, when you do an inline value (interpolated into the HTML), do it with the ternary operator:

value=”<?=$row ? $row['name'] : “”?>”

The ternary operator is a standard construct in most programming languages (VB6 had IIF, but it’s the same thing). The Syntax of the ternary operator is:

condition ? truepart : falsepart

Where condition is any valid statement that translates into a boolean, truepart is what to do when it’s true, and falsepart is what to do when it’s false.

Let’s take something a teensy bit more complicated. If you have a date in the database (let’s say a DateTime type), and you want to initialize to the current date if the date in the table is empty, otherwise print the date as m/d/Y, the ternary operators can be nested like so:

value=”<?=$row ? $row['date'] ? date(’m/d/Y’, strtotime($row['date'])) : date(’m/d/Y’) : date(’m/d/Y’)?>”

Confusing? Not so much. If we use some parentheses to section it off, it looks like:

value=”<?=”$row ? ($row['date'] ? date(’m/d/Y’, strtotime($row['date'])) : date(’m/d/Y’) ) : date(’m/d/Y’)?>”

Which is the same thing as doing this (in blocks):

if ($row):
   if ($row['date']):
     print date(’m/d/Y’, strtotime($row['date']));
   else:
     print date(’m/d/Y’);
   endif;
else:
   print date(’m/d/Y’);
endif;

Interpolation of PHP into HTML gets a bad rap either way, because it makes code harder to maintain, and a little ugly. But if you’re going to do it, and many of us still do (I don’t want to have the overhead and code size of something like Smarty all the time), then do it well and correctly.

And correctly includes the use ofthe htmlspecialchars() function, but that’s a topic for another day.

~A!

WordPress Themes