Open In App

Ternary operator vs Null coalescing operator in PHP

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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
 
// 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";
?>


Output:

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
 
// PHP program to use Null
// Coalescing Operator
 
// Assign value to variable
$num = 10;
 
// Use Null Coalescing Operator
// and display result
print ($num) ?? "NULL";
 
?>


Output:

10

Difference between Ternary operator and Null coalescing operator:

  • Ternary Operator is left associative whereas Null Coalescing operator is right associative.
  • The ternary operator is represented by ?: whereas Null Coalescing operator is represented by ??
  • 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.
  • Null Coalescing is also like “GATE” that will only lets NULL through whereas ?: is also like a “GATE” that lets anything false through – including NULL.
  • The Use of Null coalescing operator is to check  !isset() || is_null()  whereas the use of ternary operator is to 
    check for empty(), !isset(), is_null().


Last Updated : 09 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads