If-else and Switch cases are used to evaluate conditions and decide the flow of a program. The ternary operator is a shortcut operator used for shortening the conditional statements.
ternary operator: The ternary operator (?:) is a conditional operator used to perform a simple comparison or check on a condition having simple statements. It decreases the length of the code performing conditional operations. The order of operation of this operator is from left to right. It is called a ternary operator because it takes three operands- a condition, a result statement for true, and a result statement for false. The syntax for the ternary operator is as follows.
Syntax:
(Condition) ? (Statement1) : (Statement2);
- Condition: It is the expression to be evaluated and 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.
The result of this comparison can also be assigned to a variable using the assignment operator. The syntax is as follows:
Variable = (Condition) ? (Statement1) : (Statement2);
If the statement executed depending on the condition returns any value, it will be assigned to the variable.
Advantages of Ternary Operator: Following are some advantages of ternary operator:
- The use of the ternary operator will make the code shorter in comparison to the IF ELSE statement.
- The code can be quick in length in comparison to the IF ELSE statement.
- The readability of the code will increase with the usage of conditional statements.
- The use of the ternary operator makes the code simpler.
Example 1: In this example, if the value of $a is greater than 15, then 20 will be returned and will be assigned to $b, else 5 will be returned and assigned to $b.
php
<?php
$a = 10;
$b = $a > 15 ? 20 : 5;
print ("Value of b is " . $b);
?>
Output:
Value of b is 5
Example 2: In this example, if the value of $age is more than or equal to 18, "Adult" is passed to print function and printed, else "Not Adult" is passed and printed.
php
<?php
$age = 20;
print ($age >= 18) ? "Adult" : "Not Adult";
?>
Output:
Adult
When we use ternary operator: We use the ternary operator when we need to simplify the if-else statements that are simply assigning values to variables depending on a condition. An advantage of using a ternary operator is that it reduces the huge if-else block to a single line, improving the code readability and simplify it. Very useful while assigning variables after form submission.
Example:
Original Code:
php
<?php
if(isset($_POST['Name']))
$name = $_POST['Name'];
else
$name = null;
if(isset($_POST['Age']))
$age = $_POST['Age'];
else
$age = null;
?>
Reduced to the following: Thus, the ternary operator successfully reduces the if-else block to a single line, hence serving its purpose.
php
<?php
$name = isset($_POST['Name'])?$_POST['Name']:null;
$age = isset($_POST['Age'])?$_POST['Age']:null;
?>
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.
Similar Reads
Scope Resolution operator in PHP The scope resolution operator also known as Paamayim Nekudotayim or more commonly known as the double colon is a token that allows access to static, constant, and overridden properties or methods of a class. It is used to refer to blocks or codes in context to classes, objects, etc. An identifier is
2 min read
How to use Modulo Operator in PHP ? The modulo operator in PHP is represented by the percentage symbol %. It calculates the remainder of one number divided by another. The basic syntax for the modulo operator is- Syntax: $result = $num1 % $num2;Here, $num1 is the dividend, and $num2 is the divisor. The result will be the remainder aft
1 min read
Ternary operator vs Null coalescing operator in PHP Ternary Operator Ternary operator is the conditional operator which helps to cut the number of lines in the coding while performing comparisons and conditionals. It is an alternative method of using if else and nested if else statements. The order of execution is from left to right. It is absolutely
3 min read
How to Find Largest Number using Ternary Operator in PHP? Given three different numbers, the task is to find the Largest Number using Ternary Operator in PHP. In PHP, the ternary operator (?:) allows you to make a quick decision based on a condition. It is a shorthand method to write an if...else statement. You can use the ternary operator to find the larg
2 min read
Double not (!!) operator in PHP The "NOT NOT" operator or Double not(!!) operator in PHP simply returns the truth value of the variable or expression. To explain in very simple terms, the first not operator(!) negates the expression. The second not operator(!) again negates the expression resulting the true value which was present
2 min read
Uppercase Booleans vs. Lowercase in PHP The boolean expresses the truth value. The Boolean represents two possible values: TRUE or FALSE. True can be given a value of 1, and False is given a value of zero. To specify a boolean literal, use the constants TRUE or FALSE. Both are case-insensitive. It means that TRUE is equal to true and FALS
2 min read