Ternary operator vs Null coalescing operator in PHP

04 Jan, 2019
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 resultprint ($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 resultprint ($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.
Picked
Technical Scripter 2018
PHP
PHP Programs
Technical Scripter
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to convert array to string in PHP ?
PHP | Converting string to Date and DateTime
Comparing two dates in PHP
How to receive JSON POST with PHP ?
Split a comma delimited string into an array in PHP
How to convert array to string in PHP ?
How to call PHP function on the click of a Button ?
Comparing two dates in PHP
Split a comma delimited string into an array in PHP
How to get parameters from a URL string in PHP?