Spaceship operator (<=>) + PHP7
PHP7 introduce the Spaceship (<=>) operator.
This <=> operator will offer combined comparison
Return 0 if values on either side are equal
Return 1 if value on the left is greater
Return -1 if the value on the right is greater
<=> operator will be similar to in older PHP versions of below syntax,
($a < $b) ? -1 : (($a > $b) ? 1 : 0);
Now in PHP 7, it will be like,
$a <=> $b;
The rules used by the combined comparison operator are same as the currently used comparison operators by PHP with <, <=, ==, >= and >.
PERL or RUBY programming background programmers are already familiar with this new operator proposed for PHP7.
It returns,
0 if $a == $b
-1 if $a < $b
1 if $a > $b
Comparing Integers,
echo 2 <=> 2; //ouputs 0
echo 2 <=> 3; //outputs -1
echo 3 <=> 2; //outputs 1
String Comparison
echo "a" <=> "a"; // 0
echo "a" <=> "b"; //-1
echo "b" <=> "a"; //1
It can be used for sorting.
PHP7 introduce the Spaceship (<=>) operator.
This <=> operator will offer combined comparison
Return 0 if values on either side are equal
Return 1 if value on the left is greater
Return -1 if the value on the right is greater
<=> operator will be similar to in older PHP versions of below syntax,
($a < $b) ? -1 : (($a > $b) ? 1 : 0);
Now in PHP 7, it will be like,
$a <=> $b;
The rules used by the combined comparison operator are same as the currently used comparison operators by PHP with <, <=, ==, >= and >.
PERL or RUBY programming background programmers are already familiar with this new operator proposed for PHP7.
It returns,
0 if $a == $b
-1 if $a < $b
1 if $a > $b
Comparing Integers,
echo 2 <=> 2; //ouputs 0
echo 2 <=> 3; //outputs -1
echo 3 <=> 2; //outputs 1
String Comparison
echo "a" <=> "a"; // 0
echo "a" <=> "b"; //-1
echo "b" <=> "a"; //1
It can be used for sorting.
No comments:
Post a Comment