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 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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.