Tuesday, 6 April 2021

Php: <=>: Spaceship operator

Spaceship operator return

a.   0 if both the operands are same

b.   -1 if the left operand is less than right one

c.    1 if the left operand is greater than right one

 

spaceship_operator_demo.php

#!/usr/bin/php

<?php
$a = 11 <=> 11; // 0
$b = 11 <=> 22; // -1
$c = 22 <=> 11; // 1

$d = 'a' <=> 'a'; // 0
$e = 'a' <=> 'b'; // -1
$f = 'b' <=> 'a'; // 1

echo "11 <=> 11 : $a \n";
echo "11 <=> 22 : $b \n";
echo "22 <=> 11 : $c \n\n";

echo "'a' <=> 'a' : $d \n";
echo "'a' <=> 'b' : $e \n";
echo "'b' <=> 'a' : $f \n";

?>

 

Output

$./spaceship_operator_demo.php 

11 <=> 11 : 0 
11 <=> 22 : -1 
22 <=> 11 : 1 

'a' <=> 'a' : 0 
'a' <=> 'b' : -1 
'b' <=> 'a' : 1

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment