Fri 21 Jul 2006
I’ve been using PHP for years, but had never used the quick if construct:
$var = (conditional) ? ‘true’ : ‘false’;
?>
Beautifully simple; but not in any of the books I used to learn PHP!
Technorati Tags:
server, developing, php
August 2nd, 2006 at 1:00 pm
That’s a ternary operator. It’s used in a lot of languages, including JavaScript.
Personally, I like it, and I think it makes the code cleaner. Other people would strongly disagree, and thinks it makes the code harder to read.
You can get really crazy and try something like this:
$val = ($testVal) ? ‘hi’ : (($testVal2) ? ‘bye’ : ‘what?’);
You can keep chaining the operator along.
August 2nd, 2006 at 1:24 pm
Always good to have a name for these things. It\’s funny how you can be coding for years and never come across something!