What if you could compare values with just one simple symbol instead of many lines of code?
Why Spaceship operator in PHP? - Purpose & Use Cases
Imagine you need to compare two numbers to decide which one is bigger, smaller, or if they are equal. Doing this manually means writing multiple if-else statements to check each case.
Writing many if-else checks is slow and easy to mess up. It makes your code longer, harder to read, and more likely to have mistakes when comparing values.
The spaceship operator lets you compare two values in one simple step. It returns -1, 0, or 1 depending on whether the first value is less than, equal to, or greater than the second. This makes your code cleaner and easier to understand.
$a = 5; $b = 10; if ($a < $b) { return -1; } elseif ($a > $b) { return 1; } else { return 0; }
return $a <=> $b;It enables quick and clear comparisons that simplify sorting and decision-making in your code.
When sorting a list of scores, the spaceship operator helps decide the order of each pair quickly and cleanly.
Manual comparisons require many if-else statements.
Spaceship operator returns -1, 0, or 1 in one step.
This makes code shorter, clearer, and less error-prone.
Practice
<=> return when comparing two equal values?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 CQuick Check:
Equal values return 0 [OK]
- Thinking it returns true or false
- Confusing with comparison operators like == or ===
- Assuming it returns boolean instead of integer
$a and $b using the spaceship operator in PHP?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 AQuick Check:
Correct operator is <=> [OK]
- Using => which is for arrays
- Adding extra equals signs
- Using arrows like <-> which is invalid
$x = 5;
$y = 10;
echo $x <=> $y;
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 AQuick Check:
5 < 10 returns -1 [OK]
- Confusing output with boolean true or false
- Expecting the right value to print
- Thinking it returns 1 when left is smaller
$a = 7;
$b = 3;
if ($a <=> $b) {
echo 'a is greater';
}
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 DQuick Check:
Non-zero spaceship result is true in if [OK]
- Thinking spaceship operator returns boolean
- Expecting it to directly compare like > or <
- Ignoring that 0 is false and others true in if
$numbers = [3, 1, 4, 1, 5], which code snippet correctly sorts it in ascending order using the spaceship operator in a custom comparison function?Solution
Step 1: Understand sorting with spaceship operator
To sort ascending, compare $a and $b with$a <=> $b.Step 2: Check correct function usage
usortaccepts a comparison function;sortdoes not take a callback.Final Answer:
usort($numbers, fn($a, $b) => $a <=> $b); -> Option BQuick Check:
Ascending sort uses $a <=> $b with usort [OK]
- Using sort() with a callback function
- Reversing $a and $b for ascending order
- Using equality check instead of comparison
Ready to prove it?
Pass the test and this pattern moves unknown
