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 the best case time saving option. It does produces an e-notice while encountering a void value with its conditionals.
Syntax:
(Condition) ? (Statement1) : (Statement2);
In ternary operator, if condition statement is true then statement1 will execute otherwise statement2 will execute.
Alternative Method of Conditional Operation:
if (Condition) {
return Statement1;
} else {
return Statement2;
}
Example:
<?php // PHP program to check number is even // or odd using ternary operator // Assign number to variable $num = 21; // Check condition and display result print ($num % 2 == 0) ? "Even Number" : "Odd Number"; ?> |
Odd Number
Null coalescing operator
The Null coalescing operator is used to check whether the given variable is null or not and returns the non-null value from the pair of customized values. Null Coalescing operator is mainly used to avoid the object function to return a NULL value rather returning a default optimized value. It is used to avoid exception and compiler error as it does not produce E-Notice at the time of execution. The order of execution is from right to left. While execution, the right side operand which is not null would be the return value, if null the left operand would be the return value. It facilitates better readability of the source code.
Syntax:
(Condition) ? (Statement1) ? (Statement2);
Alternative method of Conditional Operation:
// The isset() function is used to take
// care that the condition is not NULL
if ( isset(Condition) ) {
return Statement1;
} else {
return Statemnet2;
}
Example:
<?PHP // PHP program to use Null // Coalescing Operator // Assign value to variable $num = 10; // Use Null Coalescing Operator // and display result print ($num) ?? "NULL"; ?> |
10
Difference between Ternary operator and Null coalescing operator:
- Ternary Operator is left associative where as, Null Coalescing operator is right associative.
- Ternary Operator throws e-notice if left operand is null, while null coalescing operator does not throws e-notice if left operand does not exist.
- Ternary Operator checks whether the value is true, but Null coalescing operator checks if the value is not null.
- If there is more iteration to be executed, null coalescing operator found to be faster than the ternary operator.
- Null coalescing operator gives better readability as well comparatively.
Recommended Posts:
- JavaScript | Nullish Coalescing Operator
- PHP | Ternary Operator
- C++ | Nested Ternary Operator
- Ternary operator / Question mark and colon in JavaScript
- Why overriding both the global new operator and the class-specific operator is not ambiguous?
- PHP | $ vs $$ operator
- 'AND' vs '&&' as operator in PHP
- set operator= in C++ STL
- What is the difference between the | and || or operator in php?
- unordered_multiset operator = in C++ STL
- PHP 7 | Spaceship Operator
- || operator in Java
- unordered_set operator= in C++ STL
- What is JavaScript >>> Operator and How to use it ?
- What does +_ operator mean in JavaScript?
- What is the !! (not not) operator in JavaScript?
- unordered_set operator!= in C++ STL
- unordered_set == operator in C++ STL
- JavaScript | in operator
- Double not (!!) operator in PHP
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

