Advertisement
Google Ad Slot: content-top
PHP Shorthand if Statements
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
<?php
$a = 19;
if ($a > 18) $b = "You are eligible to vote.";
echo $b
?>
Example of a Shorthand If...Else
<?php
$age = 18;
$message = ($age >= 18) ? "You are eligible to vote." : "You are not eligible to vote.";
echo $message;
?>
Note
This technique is known as Ternary Operators, or Conditional Expressions.