PHP Shorthand if Statements

Short Hand If

To write shorter code, you can write if statements on one line.

Example

One-line if statement:

$a = 5;

if ($a < 10) $b = "Hello";

echo $b

Short Hand If…Else

ifelse statements can also be written in one line, but the syntax is a bit different.

Example

One-line ifelse statement:

$a = 13;

$b = $a < 10 ? "Hello" : "Good Bye";

echo $b;

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