PHP and MySQL (54 Blogs) Become a Certified Professional

What is ternary operator in PHP and how is it used?

Last updated on Nov 27,2019 11.7K Views
A Data Science Enthusiast with in-hand skills in programming languages such as... A Data Science Enthusiast with in-hand skills in programming languages such as Java & Python.
What is ternary operator in PHP and how is it used?

Using the if-else and switch case is an essential part of programming for evaluating conditions. We always look for shortcuts everywhere whether it is a route for traveling or game or code. In this Ternary Operator PHP, we will see how it is used for shortening conditional statements in the following sequence:


What is Ternary operator?

The ternary operator is a conditional operator that decreases the length of code while performing comparisons and conditionals. This method is an alternative for using if-else and nested if-else statements. The order of execution for this operator is from left to right. Obviously, it is the best case for a time-saving option.

It also produces an e-notice while encountering a void value with its conditionals. It is called a ternary operator because it takes three operands – a condition, a result for true, and a result for false.


(Condition) ? (Statement1) : (Statement2);

  • Condition: It is the expression to be evaluated which returns a boolean value.
  • Statement 1: it is the statement to be executed if the condition results in a true state.
  • Statement 2: It is the statement to be executed if the condition results in a false state.

Example program to whether student is pass or fail:

print ($marks>=40) ? "pass" : "Fail";



When do we use Ternary Operator?

We use ternary operator when we need to simplify if-else statements that are used to assign values to variables. Moreover, it is commonly used when we assign post data or validate forms.

Let’s say, we were programming a login form for a college university where we wanted to ensure that the user entered their registration number provided by the university then we could move further.

//if the registration number is not specified, notify the customer
$reg_number = (isset($_POST['reg'])) ? $_POST['reg'] : die('Please enter your registration number');

Let’s look at an example of a validation form for better understanding:

<form method="post" action=”edit.php">
<label>Name<input type="text" name="name"></label><br>
<label>Email<input type="text" name="email"></label><br>
<input type="submit" name="submit" value="Submit">

In order to get the values of our text fields, we can use the following code:

$name = isset($_POST['name']) ? $_POST['name'] : null;
$email = isset($_POST['email']) ? $_POST['email'] : null;


Advantages of Ternary Operator

  • It will make the code shorter
  • It will make the code more readable
  • The code becomes simpler

Ternary shorthand

Short ternary operator syntax can be used by leaving out the middle part of the ternary operator for quick shorthand evaluation. It is also referred to as Elvis operatory(?:)


expression1 ?: expression2

Elvis operator can be used in order to reduce redundancy of your conditions and shorten the length of your assignments. It is the ternary operator with the second operand omitted. It will return the first operand if the operand is true else it evaluates and returns its second operand.

$val = $_GET['user'] ?: 'default';

If you use the ternary shorthand operator like this, it will cause notice if


is not set, instead of writing some lengthy code like this:

$val = isset($_GET['user']) ? $_GET['user'] : 'default';

Null Coalescing Operator

It replaces the ternary operation in conjunction with isset() function which is used to check whether a given variable is NULL or not and returns its first operand if it exists and is not NULL else it returns the second operand.



$user= $_GET['user'] ?? 'nobody';

It will fetch the value of $_GET[‘user’] and returns ‘nobody’ if it does not exist.

Instead of writing some lengthy code like this:

$user= isset($_GET['user']) ? $_GET['user'] : 'nobody';


With this we come to an end of this article, I hope you understood the ternary operator, the purpose and advantages of the ternary operator, Ternary shorthand and Null coalescing Operator.

If you found this Ternary Operator blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of ”ternary operator in php” and I will get back to you.


Join the discussion

Browse Categories

webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.