Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP Shorthand if

Short Hand If

To create more concise code, you can write if statements in a single line.

Example

Single-line if statement:

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

Short Hand If…Else

If…else statements can also be expressed in a single line, though the syntax differs slightly.

Example

Single-line if…else statement:

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

This technique is referred to as Ternary Operators or Conditional Expressions.