View Single Post
Old 05-18-2009, 04:58 PM   #12 (permalink)
wjgilmore
The Wanderer
 
Join Date: May 2009
Location: Columbus, Ohio
Posts: 10
Thanks: 0
wjgilmore is on a distinguished road
Default

FWIW while initially ternary operators can be a bit confusing to read, over time I've found them indispensable primarily for reason of writing compact code. For instance, I regularly use a Zend Framework view helper which will return "his" if a user object's gender attribute is set to "m", and "her" if the attribute is set to "f". Using a ternary, I can make this determination within the view helper like so:

PHP Code:
return $user->gender == "m" "his" "her"
So while indeed the ternary may be a bit of a bear to understand at first, they can wind up removing literally hundreds of lines of code otherwise required by an if statement which ultimately performs an identical task.

Just my $0.02,
Jason
==
W. Jason Gilmore
Author "Easy PHP Websites with the Zend Framework"
http://www.easyphpwebsites.com/
wjgilmore is offline  
Reply With Quote