Created
June 4, 2014 05:21
-
-
Save fmtarif/097179a8df32aa14447e to your computer and use it in GitHub Desktop.
#php Taming nested ternary, nested ternary has behavior that is not always obvious
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* nested ternary quirk | |
*/ | |
// will echo f not t, left to right association | |
echo true? 't' : false? 'f' | |
: 't'; | |
// will echo t, added braces | |
echo true? 't' | |
:(false? 'f' | |
: 't'); | |
// better syntax for readability | |
$text = 'waiting'; | |
echo ($text == 'complete')? 'complete' | |
:(($text == 'waiting')? 'waiting' | |
:(($text == 'cancelled')? 'cancelled' | |
: 'default')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment