Introduction

Thank you!

End

Spaceship Operator In PHP (<=>)

Introduction

The spaceship operator is a combined comparision operator, introduced in PHP version 7. It is a 3-way comparision operator.

 

Syntax

The syntax of spaceship operator is <=>

 

How it works?

If x and y are two variables, the x<=>y works as

If x is equal to y it returns 0, if x is greater than y it returns 1, and if x is less than y it returns -1

Example

Without Spaceship Operator

$x = 10;
$y = 15;

if ($x > $y) {
    echo 1;
} elseif ($x < $y) {
    echo -1;
} else {
    echo 0;
}

With Spaceship Operator

$x = 10;
$y = 15;

echo $x <=> $y;

 

Thank you!

I hope this article was helpful for you, do share it with your friends.

Tags