When the shorthand syntax used in the scenario, I'm trying to use it but its giving me some weird results
($value == 'yes' ?: 'Show Text');
Thanks
When the shorthand syntax used in the scenario, I'm trying to use it but its giving me some weird results
($value == 'yes' ?: 'Show Text');
Thanks
It's the binary conditional operator introduced in PHP 5.3. PHP's conditional operator is traditionally a ternary operator (accepting three operands), but the binary (accepting two operands) form was added, making the operand between the ?
and :
optional:
Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression
expr1 ?: expr3
returnsexpr1
ifexpr1
evaluates toTRUE
, andexpr3
otherwise.
So in your example, if $value
is 'yes'
, the result is TRUE (1) because the result is the first expression's value ($value == 'yes'
). If $value
is not 'yes'
, the result is 'Show Text'
.