PHP Ternary Operators

PHP Ternary If

What is a ternary operator?

PHP ternary operators are set out like the line of code below. This can make your code faster and easier to read which is a benefit when sharing code with other developers.

1
$name = (!empty($_GET['name'])? $_GET['name'] : 'John');

The statement above is translated into:

1
2
3
4
5
$name = if (!empty($_GET['name']) {
echo "$_GET['name']";
} else {
echo "'John'";
}

Hope this helps.