PHP Ternary Operator

Created with Sketch.

PHP Ternary Operator

Summary: in this tutorial, you will learn to use the PHP ternary operator to make the code shorter and more readable.

Introduction to the PHP ternary operator

The ternary operator is a shorthand for the if...else statement. Instead of writing this:

<?php

if (condition) {
$result = value1;
} else {
$result = value2;
}

Code language: HTML, XML (xml)

you can use this:

$result = condition ? value1 : value2;

Code language: PHP (php)

How it works.

  • First. PHP evaluates the condition. If it’s true, the right-hand expression returns the value1; otherwise, it returns the value2.
  • Second, PHP assigns the result of the right-hand expression to the $result variable.

As you can see, by using the ternary operator, you can make the code more concise.

Note that the name ternary operator comes from the fact that this operator requires three operands: expression, value1, value2.

PHP ternary operator example

Suppose you want to display the login link if the user has not logged in and the logout link if the user has already logged in. To do that, you can use the if...else statement as follows:

<?php

$is_user_logged_in = false;

if ($is_user_logged_in) {
$title = 'Logout';
} else {
$title = 'Login';
}

Code language: HTML, XML (xml)

In this example, the $title will be 'Login' because the $is_user_logged_in is set to false. The code is quite lengthy. And you can make it shorter by using the ternary operator as follows:

<?php

$is_user_logged_in = false;

$title = $is_user_logged_in ? 'Logout' : 'Login';

Code language: HTML, XML (xml)

It’s much shorter now. If the line is long, you can always break it down like this:

<?php

$is_user_logged_in = false;

$title = $is_user_logged_in
? 'Logout'
: 'Login';

Code language: HTML, XML (xml)

The shorthand ternary operator

Starting from PHP 5.3, you can use the shorthand ternary operator as follows:

$result = $initial ?: $default;

Code language: PHP (php)

In this syntax, PHP evaluates $initial in the boolean context. If $initial is true, PHP assigns the value of the $initial to the $result variable. Otherwise, it assigns the $default to the $result variable.

The following example uses the shorthand ternary operator to assign the value of the $path to the $url if the $path is not empty. If the $path is empty, the ternary operator assigns the literal string ‘/’ to the $url:

<?php

$path = '/about';
$url = $path ?: '/';

echo $url; // /about

Code language: HTML, XML (xml)

Output:

/about

 

Chaining ternary operators

Technically, you can chain ternary operators by using parentheses.

Suppose you want to show various messages if users are eligible and have enough credit. The following example chains two ternary operators:

<?php

$eligible = true;
$has_credit = false;

$message = $eligible
? ($has_credit
? 'Can use the credit'
: 'Not enough credit')
: 'Not eligible to buy';

echo $message;

Code language: HTML, XML (xml)

Most of the time, chaining multiple ternary operators makes the code more difficult to read. In this case, it’s better to use if...else or if...elseif statement.

Summary

  • The ternary operator (?:) is a shorthand for the if...else statement.
  • Do use the ternary operator when it makes your code more concise and more readable.

Leave a Reply

Your email address will not be published. Required fields are marked *