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 :  PHP Variabel, echo and print / PHP Dasar
Topic : Hosting | Tags : , ,
Author : Andreas

Publisher konten, aktif membuat artikel informatif membantu pembaca memahami tren terbaru secara lebih jelas dan mudah dipahami.

Editor : SEOSatu

Bertanggung jawab atas proses penyuntingan, verifikasi, dan optimasi pada setiap artikel. Memastikan konten akurat, relevan, dan sesuai standar kualitas.