CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

PHP - Spaceship Operator

PHP - Spaceship Operator

The Spaceship operator is one of the many new features introduced in PHP with its 7.0 version. It is a three-way comparison operator.

The conventional comparison operators (<, >, !=, ==, etc.) return true or false (equivalent to 1 or 0). On the other hand, the spaceship operator has three possible return values: -1,0,or 1. This operator can be used with integers, floats, strings, arrays, objects, etc.

Syntax of the Spaceship Operator

The symbol used for spaceship operator is”<=>β€œ.

PHP CODE
$retval = operand1 &lt;=&gt; operand2

Here, $retval is -1 if operand1 is less than operand2, 0 if both the operands are equal, and 1 if operand1 is greater than operand2.

The spaceship operator is implemented as a combined comparison operator. Conventional comparison operators could be considered mere shorthands for <=> as the following table shows βˆ’

Operator<=> equivalent$a < $b($a <=> $b) === -1$a <= $b($a <=> $b) === -1 || ($a <=> $b) === 0$a == $b($a <=> $b) === 0$a != $b($a <=> $b) !== 0$a >= $b($a <=> $b) === 1 || ($a <=> $b) === 0$a > $b($a <=> $b) === 1

Example 1

The following example shows how you can use the spaceship operator in PHP. We will compare 5 with 10/2. As both values are equal so the result will be 0 βˆ’

PHP CODE
&lt;?php
   $x = 5;
   $y = 10;
   $z = $x &lt;=&gt; $y/2;

   echo "$x <=> $y/2 = $z";
?&gt;

Output

It will create the following output βˆ’

PHP CODE
5 &lt;=&gt; 10/2 = 0

Example 2

Now we change $x=4 and check the result. As 4 is less than 10/2 so the spaceship operator will return -1 βˆ’

PHP CODE
&lt;?php
   $x = 4;
   $y = 10;
   $z = $x &lt;=&gt; $y/2;

   echo "$x <=> $y/2 = $z";
?&gt;

Output

It will produce the following output βˆ’

PHP CODE
4 &lt;=&gt; 10/2 = -1

Example 3

Now let us change $y=7 and check the result again. Now 7 is greater than 10/2 so the spaceship operator will return 1 βˆ’

PHP CODE
&lt;?php
   $x = 7;
   $y = 10;
   $z = $x &lt;=&gt; $y/2;

   echo "$x <=> $y/2 = $z";
?&gt;

Output

It will produce the following result βˆ’

PHP CODE
7 &lt;=&gt; 10/2 = 1

Example 4

The spaceship operator, like the strcmp() method, works on strings. In this program, we will compare the phrases β€œbat” and β€œball”. Since β€œbat” comes after β€œball” in alphabetical order so the result is 1.

PHP CODE
&lt;?php
   $x = "bat";
   $y = "ball";
   $z = $x &lt;=&gt; $y;

   echo "$x <=> $y = $z";
?&gt;

Output

It will generate the following outcome βˆ’

PHP CODE
bat &lt;=&gt; ball = 1

Example 5

Change $y = β€œbaz” and check the result βˆ’

PHP CODE
&lt;?php
   $x = "bat";
   $y = "baz";
   $z = $x &lt;=&gt; $y;

   echo "$x <=> $y = $z";
?&gt;

Output

It will produce the below output βˆ’

PHP CODE
bat &lt;=&gt; baz = -1

Spaceship Operator with Boolean Operands

The spaceship operator also works with Boolean operands βˆ’

PHP CODE
true &lt;=&gt; false returns 1
false &lt;=&gt; true returns -1
true &lt;=&gt; true as well as false &lt;=&gt; false returns 0