The spaceship operator (<=>) is a comparison operator that returns -1, 0, or 1 when comparing two values. It returns -1 if the left value is less, 0 if they are equal, and 1 if the left value is greater.
Click to reveal answer
beginner
How does the spaceship operator differ from traditional comparison operators?
Unlike <, >, or == which return boolean true or false, the spaceship operator returns an integer -1, 0, or 1 to show the order between two values.
Click to reveal answer
beginner
What will 5 <=> 10 return in PHP?
It returns -1 because 5 is less than 10.
Click to reveal answer
beginner
What will 10 <=> 10 return in PHP?
It returns 0 because both values are equal.
Click to reveal answer
beginner
What will 15 <=> 10 return in PHP?
It returns 1 because 15 is greater than 10.
Click to reveal answer
What does the spaceship operator (<=>) return when the left value is less than the right value?
A0
Btrue
C1
D-1
✗ Incorrect
The spaceship operator returns -1 when the left value is less than the right value.
What is the output of 7 <=> 7 in PHP?
A0
B1
C-1
Dfalse
✗ Incorrect
It returns 0 because both values are equal.
Which of these is true about the spaceship operator?
AIt returns an integer indicating order.
BIt returns a boolean value.
CIt only works with strings.
DIt is used for assignment.
✗ Incorrect
The spaceship operator returns -1, 0, or 1 to indicate order between two values.
What will 20 <=> 10 return?
A-1
B0
C1
Dtrue
✗ Incorrect
It returns 1 because 20 is greater than 10.
Which PHP version introduced the spaceship operator?
APHP 5.6
BPHP 7.0
CPHP 8.0
DPHP 7.4
✗ Incorrect
The spaceship operator was introduced in PHP 7.0.
Explain how the spaceship operator works and give an example comparing two numbers.
Think about how it shows if one number is less, equal, or greater than another.
You got /3 concepts.
Describe a situation where using the spaceship operator is better than using multiple comparison operators.
Consider when you want to know if one value is less, equal, or greater in a single comparison.
You got /3 concepts.
Practice
(1/5)
1. What does the PHP spaceship operator <=> return when comparing two equal values?
easy
A. 1
B. -1
C. 0
D. true
Solution
Step 1: Understand the spaceship operator behavior
The spaceship operator returns -1 if the left value is less, 0 if equal, and 1 if greater.
Step 2: Apply to equal values
When both values are equal, the operator returns 0.
Final Answer:
0 -> Option C
Quick Check:
Equal values return 0 [OK]
Hint: Equal values always return 0 with <=> operator [OK]
Common Mistakes:
Thinking it returns true or false
Confusing with comparison operators like == or ===
Assuming it returns boolean instead of integer
2. Which of the following is the correct syntax to compare two variables $a and $b using the spaceship operator in PHP?
easy
A. $a <=> $b;
B. $a => $b;
C. $a <==> $b;
D. $a <-> $b;
Solution
Step 1: Recall the spaceship operator syntax
The spaceship operator is written as <=> between two values.
Step 2: Check each option
$a <=> $b; uses <=> correctly. Others have invalid or incorrect symbols.
Final Answer:
$a <=> $b; -> Option A
Quick Check:
Correct operator is <=> [OK]
Hint: Spaceship operator is exactly <=> with no extra symbols [OK]
Common Mistakes:
Using => which is for arrays
Adding extra equals signs
Using arrows like <-> which is invalid
3. What is the output of the following PHP code?
$x = 5; $y = 10; echo $x <=> $y;
medium
A. -1
B. 1
C. 0
D. 10
Solution
Step 1: Compare values 5 and 10 using spaceship operator
Since 5 is less than 10, the operator returns -1.
Step 2: Output the result
The echo statement prints -1.
Final Answer:
-1 -> Option A
Quick Check:
5 < 10 returns -1 [OK]
Hint: Left less than right returns -1 with <=> [OK]
Common Mistakes:
Confusing output with boolean true or false
Expecting the right value to print
Thinking it returns 1 when left is smaller
4. Identify the error in the following PHP code using the spaceship operator:
$a = 7; $b = 3; if ($a <=> $b) { echo 'a is greater'; }
medium
A. Variables $a and $b are not defined
B. Spaceship operator cannot be used in if condition
C. Missing semicolon after echo statement
D. The condition always evaluates to true except when values are equal
Solution
Step 1: Understand spaceship operator in if condition
The operator returns -1, 0, or 1. In PHP, 0 is false, others are true.
Step 2: Analyze the condition with $a=7 and $b=3
Since 7 > 3, it returns 1, so condition is true and echo runs.
Final Answer:
The condition always evaluates to true except when values are equal -> Option D
Quick Check:
Non-zero spaceship result is true in if [OK]
Hint: Spaceship returns 0 (false) only if values equal [OK]
Common Mistakes:
Thinking spaceship operator returns boolean
Expecting it to directly compare like > or <
Ignoring that 0 is false and others true in if
5. Given an array $numbers = [3, 1, 4, 1, 5], which code snippet correctly sorts it in ascending order using the spaceship operator in a custom comparison function?
hard
A. usort($numbers, fn($a, $b) => $a == $b);
B. usort($numbers, fn($a, $b) => $a <=> $b);
C. sort($numbers, fn($a, $b) => $a <=> $b);
D. usort($numbers, fn($a, $b) => $b <=> $a);
Solution
Step 1: Understand sorting with spaceship operator
To sort ascending, compare $a and $b with $a <=> $b.
Step 2: Check correct function usage
usort accepts a comparison function; sort does not take a callback.
Final Answer:
usort($numbers, fn($a, $b) => $a <=> $b); -> Option B
Quick Check:
Ascending sort uses $a <=> $b with usort [OK]
Hint: Use usort with fn($a,$b) => $a <=> $b for ascending sort [OK]
Common Mistakes:
Using sort() with a callback function
Reversing $a and $b for ascending order
Using equality check instead of comparison
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.