Advertisement

Google Ad Slot: content-top

PHP Shorthand if Statements

~1 min read · PHP
On this page

This is used to simplify simple if...else conditions. It condenses the if...else logic into a single line of code, making your code more compact and sometimes easier to read.



Example of a Shorthand if
18) $b = "You are eligible to vote."; echo $b ?>
Try it yourself
Example of a Shorthand If...Else
= 18) ? "You are eligible to vote." : "You are not eligible to vote."; echo $message; ?>
Try it yourself

Note

This technique is known as Ternary Operators, or Conditional Expressions.