Jun 30 2009

I’m an advocate of tidy, well commented code, and something that has bugged me for a little while was the messiness of the ternary operator (?:) in PHP. With the introduction of PHP 5.3.0, we can now miss out the duplication messiness of using the ternary operator for existance checks for example. Instead of:

$value = $a_value ? $a_value : $b_value;

We can now simply do:

$value = $a_value ?: $b_value;

It goes without saying that one should still not nest ternary operators as they become messy and extremely difficult to understand, even with good commenting. Consider an if/elseif/else or switch instead.

There are a few other things in PHP 5.3.0 that I’m looking forward to as well, one of which is the bundling of ext/phar which is really rather cool.

There are other dubious things, which are covered (and argued) in great detail elsewhere such as the goto operator and the introduction of namespaces

2 Responses to “Awesome: Short Ternary Operators”

  1. Benjie Gillam says:

    Awesome, I did not know about this! I’m still coding for 5.2 though :(

  2. James says:

    Aye me too :/

Leave a Reply