0
0
PHPprogramming~20 mins

Spaceship operator in PHP - Practice Problems & Coding Challenges

Choose your learning style9 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.