Full Stack Web Development Internship Program
- 29k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
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.
Syntax:
(Condition) ? (Statement1) : (Statement2);
Example program to whether student is pass or fail:
<?php $marks=40; print ($marks>=40) ? "pass" : "Fail"; ?>
pass
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"> </form>
In order to get the values of our text fields, we can use the following code:
<?php if(isset($_POST['submit'])) { $name = isset($_POST['name']) ? $_POST['name'] : null; $email = isset($_POST['email']) ? $_POST['email'] : null; } ?>
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(?:)
Syntax:
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
$_GET['user']
is not set, instead of writing some lengthy code like this:
$val = isset($_GET['user']) ? $_GET['user'] : 'default';
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.
Syntax:
(Condition)?(Statement1)?(Statement2);
$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.
edureka.co