Almost all the programs require evaluating conditions using if/else and switch/case statements. If-Else statements are easy to code and global to all languages. However, they can be too long. The ternary operator (?:) can replace a single if/else clause.
Ternary operator logic is the process of using "(condition) ? (true return value) : (false return value)" statements to shorten your if-else structures.
/* basic usage */
$var = 7;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
There are many advantages of using this type of logic:
Example 1: Message according to user's login status.
/* user basic usage */
$message = 'Hello '.($user->is_logged_in() ? $user->get('first_name') : 'Guest');
/* echo, inline */
echo 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody');