Bird
Raised Fist0
PHPprogramming~20 mins

Spaceship operator in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Spaceship Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP code using the spaceship operator?
Consider the following PHP code snippet. What will it output?
PHP
<?php
$a = 5;
$b = 10;
echo $a <=> $b;
?>
A-1
B1
C0
DError
Attempts:
2 left
💡 Hint
The spaceship operator returns -1 if the left is less than the right.
❓ Predict Output
intermediate
2:00remaining
What does this PHP code output when comparing equal values?
Look at this PHP code using the spaceship operator. What will it print?
PHP
<?php
$x = 7;
$y = 7;
echo $x <=> $y;
?>
Anull
B-1
C1
D0
Attempts:
2 left
💡 Hint
The spaceship operator returns 0 when both sides are equal.
❓ Predict Output
advanced
2:00remaining
What is the output of this PHP code comparing a string and an integer?
Consider the following PHP code snippet. What will it output?
PHP
<?php
$a = 'apple';
$b = 5;
echo $a <=> $b;
?>
A0
B-1
C1
DError
Attempts:
2 left
💡 Hint
When comparing a string to an integer, the string is converted to a number. 'apple' converts to 0.
❓ Predict Output
advanced
2:00remaining
What is the output of this PHP code comparing arrays with the spaceship operator?
Analyze this PHP code and determine its output.
PHP
<?php
$arr1 = [1, 2, 3];
$arr2 = [1, 2, 4];
echo $arr1 <=> $arr2;
?>
AError
B0
C-1
D1
Attempts:
2 left
💡 Hint
The spaceship operator compares arrays element by element.
🧠 Conceptual
expert
2:00remaining
What is the value of $result after this PHP code runs?
Given this PHP code, what is the value stored in $result?
PHP
<?php
function compareValues(mixed $a, mixed $b): int {
    return $a <=> $b;
}

$result = compareValues(3.5, 3);
?>
A1
B-1
C0
DTypeError
Attempts:
2 left
💡 Hint
The spaceship operator works with floats and integers.

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

  1. 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.
  2. Step 2: Apply to equal values

    When both values are equal, the operator returns 0.
  3. Final Answer:

    0 -> Option C
  4. 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

  1. Step 1: Recall the spaceship operator syntax

    The spaceship operator is written as <=> between two values.
  2. Step 2: Check each option

    $a <=> $b; uses <=> correctly. Others have invalid or incorrect symbols.
  3. Final Answer:

    $a <=> $b; -> Option A
  4. 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

  1. Step 1: Compare values 5 and 10 using spaceship operator

    Since 5 is less than 10, the operator returns -1.
  2. Step 2: Output the result

    The echo statement prints -1.
  3. Final Answer:

    -1 -> Option A
  4. 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

  1. Step 1: Understand spaceship operator in if condition

    The operator returns -1, 0, or 1. In PHP, 0 is false, others are true.
  2. Step 2: Analyze the condition with $a=7 and $b=3

    Since 7 > 3, it returns 1, so condition is true and echo runs.
  3. Final Answer:

    The condition always evaluates to true except when values are equal -> Option D
  4. 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

  1. Step 1: Understand sorting with spaceship operator

    To sort ascending, compare $a and $b with $a <=> $b.
  2. Step 2: Check correct function usage

    usort accepts a comparison function; sort does not take a callback.
  3. Final Answer:

    usort($numbers, fn($a, $b) => $a <=> $b); -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard