paint-brush
Shorthand Comparisons using Ternary Operators in PHPby@cveasey
280 reads

Shorthand Comparisons using Ternary Operators in PHP

by Clint VeaseyFebruary 16th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

This article is aimed at beginners or anyone who may be unaware of what a Ternary Operator is. I’ve encountered a few peers who seemed completely unaware that these things exist! Ternary Comparisons are very situational but can make your code a bit more readable if used in the right situation. For the uninitiated a ternary operator looks like the following:

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Shorthand Comparisons using Ternary Operators in PHP
Clint Veasey HackerNoon profile picture

This article is aimed at beginners or anyone who may be unaware of what a Ternary Operator is. I’ve encountered a few peers who seemed completely unaware that these things exist!


Ternary Comparisons are very situational but can make your code a bit more readable if used in the right situation.


For the uninitiated a ternary operator looks like the following:


/**
 * where are the kids going to play? 
 * let's look at the weather today.
 * 、ヽ`、ヽ`个c(゚∀゚∩)`ヽ、`ヽ、
 */

// ternary operator
$playArea = ($raining) ? "Inside" : "Outside"; 

// same logic using more traditional conditional statement
$playArea = "Outside";
if ($raining == True) {
  $playArea = "Inside";
}

// same logic using more traditional conditional statement
if ($raining) {
  $playArea = "Inside";
} else {
  $playArea = "Outside";
}


For example, let’s say we have a template that shows two different strings in a div depending on a session variable.


If a user is logged in we will welcome them back, otherwise, we will show our corporate slogan “A brand new start”.


First, we will use conditional statements commonly seen inserted between HTML code:


<?
// is user logged in?
session_start();
$isUserLoggedIn = $_SESSION["isUserLoggedIn "];
?>

<!-- welcome hero header -->
<div class="main">
  <div class="hero welcome">
    <? if($isUserLoggedIn): ?> 
      A brand new start.
    <?php else: ?>
      Welcome back!
    <?php endif; ?>
  </div>
</div>


Let’s revisit this example with an inline Ternary Operator condition:


<?
// is user logged in?
session_start();
$isUserLoggedIn = $_SESSION["isUserLoggedIn "];
?>

<!-- welcome hero header -->
<div class="main">
  <div class="hero welcome">
    <?= ($isUserLoggedIn) ? "A brand new start." : "Welcome back!" ?>
  </div>
</div>


Quick Note: I am unable to find a definitive answer to the correct semantics regarding the correct use of the internet’s interchangeable use of “Ternary Operator”, “Ternary Expression” and “Ternary Conditional” in regards to its use within PHP development.


I hope that adds a new tool to your toolbox, or maybe sheds light on someone else’s code that might be twisting your melon.

Further reading

PHP conditionals and comparison from the official documentation:

https://www.php.net/manual/en/language.operators.comparison.php