?:
is like a gate that letsanything falsy
through – includingNULL
- Anything falsy:
0
,empty string
,NULL
,false
,!isset()
,empty()
- Same like old ternary operator:
X ? Y : Z
- Note:
?:
will throwPHP NOTICE
on undefined (unset
or!isset()
) variables
Use of ?:
- checking
empty()
,!isset()
,is_null()
etc - shorten ternary operation like
!empty($x) ? $x : $y
to echo$x ?: $y
- shorten
if(!$x) { echo $x; } else { echo $y; }
to echo$x ?: $y