0
0
PHPprogramming~10 mins

Spaceship operator in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to compare two numbers using the spaceship operator.

PHP
<?php
$result = 5 [1] 3;
echo $result;
?>
Drag options to blanks, or click blank then click option'
A===
B!=
C==
D<=>
Attempts:
3 left
💡 Hint
Common Mistakes
Using == or === instead of the spaceship operator.
Using != which only checks inequality.
2fill in blank
medium

Complete the code to compare two variables using the spaceship operator.

PHP
<?php
$a = 10;
$b = 20;
$result = $a [1] $b;
echo $result;
?>
Drag options to blanks, or click blank then click option'
A==
B<=>
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using > or < which return boolean, not -1, 0, or 1.
Using == which only checks equality.
3fill in blank
hard

Fix the error in the code by replacing the incorrect operator with the spaceship operator.

PHP
<?php
$x = 7;
$y = 7;
$result = $x [1] $y;
echo $result;
?>
Drag options to blanks, or click blank then click option'
A<=>
B==
C!=
D===
Attempts:
3 left
💡 Hint
Common Mistakes
Using == or === which return true or false, not -1, 0, or 1.
Using != which checks inequality.
4fill in blank
hard

Fill both blanks to create a dictionary that maps numbers to their comparison result with 10 using the spaceship operator.

PHP
<?php
$numbers = [8, 10, 12];
$results = [
    [1] => [2] <=> 10,
    10 => 10 <=> 10,
    12 => 12 <=> 10
];
print_r($results);
?>
Drag options to blanks, or click blank then click option'
A8
B==
C<=>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of <=> for comparison.
Using != which is not a comparison operator for ordering.
5fill in blank
hard

Fill all three blanks to create an array that stores the comparison results of each number with 15 using the spaceship operator.

PHP
<?php
$values = [10, 15, 20];
$comparisons = [
    [1] => [1] <=> 15,
    {{BLANK_3}} => {{BLANK_3}} <=> 15,
    20 => 20 <=> 15
];
print_r($comparisons);
?>
Drag options to blanks, or click blank then click option'
A10
B<=>
C15
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of <=> for comparison.
Mixing up keys and values in the array.