- Null coalescing operator ( ?? ) : This operator is a replacement of ternary operator in conjunction with isset(). It returns its first operand if it exists and is not
NULL
; otherwise it returns its second operand
If you are aware about ternary operator then well an good if not then I am giving you one example throw which you can understand and then I will try to explain how does Null coalescing operator work.
Example Of Ternary Operator:
Syntax: statement1 ? statement2 : statement3;
If statement1 is true then statement2 will be executed otherwise statement3 will be executed.
<?php
$a = 100;
echo $a ? $a : 'statement2';
Output : 100
<?php
$a = '';
echo $a ? 'statement1' : 'statement2';
Output : statement2
<?php
$a = '';
echo isset($a) ? 'statement1' : 'statement2'; Output : statement1
<?php
echo $a ? 'statement1' : 'statement2';
Output : Notice undefined variable a;
<?php
$a=null;
echo $a ? 'statement1' : 'statement2';
Output : statement2;
Example Of Null coalescing operator: Take the above example to understand the null coalescing operator.
Syntax: statement1 ?? statement2;
So in this case if statement1 exist and not null then return statement1 otherwise return statement2.
<?php
$a = 100;
echo $a ?? 'statement1';
Output : 100
<?php
$a = '';
echo $a ?? 'statement1';
Output : in this case blank space will print because of " ??" operator will check isset and not null
<?php
echo $a ?? 'statement1';
Output : statement1
Note: In case of ternary operator it will throw Notice error in the above example but in case of Null coalescing operator , it will not throw error because of this operator will check isset as well as null at time.
<?php
$a = null;
echo $a ?? 'statement1';
Output : statement1 ( Second operand executed because of $a is NULL)
So I have tried to explain purpose and use of Null coalescing operator with the help of practical example .
2. Spaceship operator ( <=> ) : This operator works on two operands, if first operand is greater than second operand then this operator return 1, if first operand less than second operand then it return -1 and both operand equal then it return 0.
I have tried to explain the use of spaceship operator with the help of below example.
<?php
// Comparison with integer values
$a = 10;
$b = 5;
if (($a <=> $b)==1) // this comparison return 1 because of a is greater than b
echo "Variable a is greater than variable b";
// Output is : Variable a is greater than variable b
if (($b <=> $a)==-1) // this comparison return -1
echo "<br>Variable b is less than variable a";
// Output is : Variable b is less than variable a
$a = 5;
$b = 5;
if (($a <=> $b) == 0) // this comparison return 0
echo "<br>Variable a and variable b are equal";
// Output is : Variable a and variable b are equal
// comparison with float values
$a = 10.01;
$b = 10.001;
if (($a <=> $b)==1) // this comparison return 1
echo "<br><br>Variable a is greater than variable b";
// Output is : Variable a is greater than variable b
if (($b <=> $a)==-1) // this comparison return -1
echo "<br>Variable b is less than variable a";
// Output is : Variable b is less than variable a
$a = 10.00;
$b = 10.00;
if (($a <=> $b) == 0) // this comparison return 0
echo "<br>Variable a and variable b are equal";
// Output is : Variable a and variable b are equal
// String comparison
$a = "applered";
$b = "apple";
if (($a <=> $b)==1) // this comparison return 1
echo "<br><br>Variable a is greater than variable b";
// Output is : Variable a is greater than variable b
if (($b <=> $a)==-1) // this comparison return -1
echo "<br>Variable b is less than variable a";
// Output is : Variable b is less than variable a
$a = "apple";
$b = "apple";
if (($a <=> $b) == 0) // this comparison return 0
echo "<br>Variable a and variable b are equal";
// Output is : Variable a and variable b are equal
// Array comparison with integer values
$a = array(1,2,4,4); // Number of elements are 4 and the sum of elements is 11
$b = array(1,2,3,4); // Number of elements are 4 and the sum of elements is 10
if (($a <=> $b)==1) // this comparison return 1
echo "<br><br>Variable a is greater than variable b";
// Output is : Variable a is greater than variable b
if (($b <=> $a)==-1) // this comparison return -1
echo "<br>Variable b is less than variable a";
// Output is : Variable b is less than variable a
$a = array(1,2,3,4);
$b = array(1,2,3,4);
if (($a <=> $b) == 0) // this comparison return 0
echo "<br>Variable a and variable b are equal";
// Output is : Variable a and variable b are equal
// Array comparison with string values
$a = array("a","b","d"); // Number of elements are 3
$b = array("a","b","c"); // Number of elements are 3
if (($a <=> $b)==1) // this comparison return 1 because of both array have a and b which are same but the thirst element in $a has d which have the higher value than c of array $b
echo "<br><br>Variable a is greater than variable b";
// Output is : Variable a is greater than variable b
if (($b <=> $a)==-1) // this comparison return -1
echo "<br>Variable b is less than variable a";
// Output is : Variable b is less than variable a
$a = array("a","b","c");
$b = array("a","b","c");
if (($a <=> $b) == 0) // this comparison return 0
echo "<br>Variable a and variable b are equal";
// Output is : Variable a and variable b are equal
// Object comparison
$a = (object) ["a" => "b"];
$b = (object) ["a" => "a"];
if (($a <=> $b)==1) // this comparison return 1
echo "<br><br>Variable a is greater than variable b";
// Output is : Variable a is greater than variable b
if (($b <=> $a)==-1) // this comparison return -1
echo "<br>Variable b is less than variable a";
// Output is : Variable b is less than variable a
$a = (object) ["a" => "a"];
$b = (object) ["a" => "a"];
if (($a <=> $b) == 0) // this comparison return 0
echo "<br>Variable a and variable b are equal";
//Output is : Variable a and variable b are equal
If you want to check the above example then please copy and past to your PHP IDE.