Ternary operator versus zero concatenation operator in PHP


Ternary operator - it is a conditional operator that helps reduce the number of lines of code when doing comparisons and conditionals. This is an alternative method of using if else statements and nested if else statements. The order of execution is from left to right. This is absolutely the best time-saver. It does generate email notification when encountering a blank value with its conventions.Syntax:
(Condition)? (Statement1): (Statement2);
In ternary statement, if statement condition is true, statement 1 will be executed, otherwise statement 2 will be executed.Alternative method of conditional operation :
if (Condition) {return Statement1; } else {return Statement2; }
Example :  
// PHP program to check the number
// or the odd use of the ternary operator 
// Assign a variable number $num = 21;  
// Check the status and display the result print ( $num % 2 == 0)? "Even Number" : "Odd Number" ; ?>
Odd Number 
Null union operator The Null union operator is used to test if a given variable is null or not and returns a nonzero value from the configured value pair. The Null Coalescing operator is mainly used to prevent an object function from returning a NULL value, but rather to return an optimized default value. It is used to avoid compiler exceptions and errors, since it does not generate an E-Notice at runtime. The order of execution - from right to left. On execution, the right operand that is not null will be the return value, if null, the left operand will be the return value. This contributes to 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 program to use Null
/ / The union operator 
// Assign a value to the variable $num = 10;  
// Using the null concatenation operator
// and display the result print ( $num ) ?? "NULL" ;  
The difference between the ternary operator and the Null coalescing operator:
  • The ternary operator is left associative while the Null Coalescing operator is right associative.
  • The ternary operator emits an email notification if the left operand is null, whereas the null-concatenated operator does not issue an email notification if the left operand does not exist.
  • The ternary operator checks if a value is true, but the operator zeros concatenation checks if the value is not null.
  • If more iterations are required, the zero concatenation operator will be faster than the ternary operator.
  • The null concatenation operator provides better readability, and also comparatively.

Get Solution for free from DataCamp guru