Skip to content

Instantly share code, notes, and snippets.

@fmtarif
Created June 4, 2014 05:21
Show Gist options
  • Save fmtarif/097179a8df32aa14447e to your computer and use it in GitHub Desktop.
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
<?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