Ternary Operator ?: PHP Short Code (IF ELSE)

  • ?: is like a gate that lets anything falsy through – including NULL
  • Anything falsy: 0empty stringNULLfalse!isset()empty()
  • Same like old ternary operator: X ? Y : Z
  • Note: ?: will throw PHP 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
READ :  7+ How to increase organic traffic to website (blog)