0
0
PHPprogramming~10 mins

Comparison operators (loose and strict) 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 check if two variables are equal using loose comparison.

PHP
<?php
$a = 5;
$b = '5';
if ($a [1] $b) {
    echo 'Equal';
} else {
    echo 'Not equal';
}
?>
Drag options to blanks, or click blank then click option'
A==
B===
C!=
D!==
Attempts:
3 left
💡 Hint
Common Mistakes
Using === instead of == causes the comparison to fail because types differ.
2fill in blank
medium

Complete the code to check if two variables are equal using strict comparison.

PHP
<?php
$x = 10;
$y = '10';
if ($x [1] $y) {
    echo 'Strictly equal';
} else {
    echo 'Not strictly equal';
}
?>
Drag options to blanks, or click blank then click option'
A!==
B==
C!=
D===
Attempts:
3 left
💡 Hint
Common Mistakes
Using == instead of === causes the comparison to ignore type differences.
3fill in blank
hard

Fix the error in the code to correctly check if two variables are not equal using strict comparison.

PHP
<?php
$a = 7;
$b = '7';
if ($a [1] $b) {
    echo 'Not strictly equal';
} else {
    echo 'Strictly equal';
}
?>
Drag options to blanks, or click blank then click option'
A!==
B==
C!=
D===
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of !== causes type differences to be ignored.
4fill in blank
hard

Fill both blanks to create a dictionary (associative array) with keys as numbers and values as their strict equality check with 10.

PHP
<?php
$results = [
    8 => (8 [1] 10),
    10 => (10 [2] 10)
];
print_r($results);
?>
Drag options to blanks, or click blank then click option'
A===
B==
C!==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing == and === causes inconsistent results.
5fill in blank
hard

Fill all three blanks to create an array filtering numbers strictly greater than 5 and loosely equal to '7'.

PHP
<?php
$numbers = [3, 5, 7, '7', 10];
$filtered = array_filter($numbers, function($num) {
    return $num [1] 5 && $num [2] '7' && $num [3] 7;
});
print_r($filtered);
?>
Drag options to blanks, or click blank then click option'
A>
B==
C===
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > changes the filter logic.
Mixing up == and === causes unexpected results.