Backend Development

Learn About PHP Spaceship <=> Operator

Learn About PHP Spaceship _=_ Operator

In PHP usually we are doing comparisons that evaluate if two values equal, less than or greater than using if else statement, however in PHP there is spaceship operator <=> to accomplish such thing.

 

 

The spaceship operator written as <=> comes with PHP 7 is an excellent operator as it can shortens big blocks of code. This operator can be used in cases when you compare two values and check for equality, or whether certain value greater than or less than the other.

<=> operator uses exactly the same comparison rules as used by our existing comparison operators: <, <=, ==, >= and >

Consider this example:

$value1 = 10;
$value2 = 6;

if($value1 > $value2) {
    echo 1;
} else if($value1 < $value2) {
    echo -1;
} else {
    echo 0;
}

This block of code can be rewritten using <=> operator like so:

$value1 = 6;
$value2 = 10;

echo $value1 <=> $value2;

The two blocks will output the same value, however the second version is much shorter. So the spaceship operator compares two expressions and returns -1, 0 or 1 when first expression is respectively less than, equal to, or greater than second expression.

The spaceship operator <=> can also be used to compare other data types like floats, strings, arrays.

Floats Comparison

$value1 = 100.2;
$value2 = 70.5;

echo $value1 <=> $value2;   // 1
$value1 = 200.5;
$value2 = 200.5;

echo $value1 <=> $value2;   // 0
$value1 = 70.6;
$value2 = 90.2;

echo $value1 <=> $value2;   // -1

Strings Comparison

$value1 = "a";
$value2 = "c";

echo $value1 <=> $value2;   // -1
$value1 = "b";
$value2 = "a";

echo $value1 <=> $value2;   // -1
$value1 = "b";
$value2 = "b";

echo $value1 <=> $value2;   // 0
$value1 = "foo";
$value2 = "bar";

echo $value1 <=> $value2;   // 1

Comparison for strings will go from left to right and compare each character in the given string to see if they are different and compares the ASCII value of the last character.

 

Array Comparison

echo [] <=> []; // 0
echo [1, 2, 3] <=> [1, 2, 3]; // 0
echo [1, 2, 3] <=> []; // 1
echo [1, 2, 3] <=> [1, 2, 1]; // 1

Array comparison works by first checking for the array size and second the number sequence for numeric arrays.

From this knowledge of <=> operator we can use it with sorting functions that accept a callback.

As an example code:

function order_callback($a, $b) {
    return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);
}

$a = array(3, 2, 5, 6, 1);

usort($a, "order_callback");

The order_callback() should return -1, 0, or 1 respectively for the sorting functionality to work. Now we can refactor this function using spaceship <=> operator:

function order_callback($a, $b) {
    return $a <=> $b;
}

var_dump($a);

// output
array(5) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(5)
  [4]=>
  int(6)
}

 

0 0 votes
Article Rating

What's your reaction?

Excited
0
Happy
0
Not Sure
0
Confused
0

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments