0
0
PHPprogramming~10 mins

Loose comparison vs strict comparison in PHP - Interactive Practice

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

Complete the code to perform a loose comparison between two variables.

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 strict comparison, which also checks type.
2fill in blank
medium

Complete the code to perform a strict comparison between two variables.

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

Fix the error in the code to correctly check if two variables are not strictly equal.

PHP
<?php
$a = 0;
$b = false;
if ($a [1] $b) {
    echo "Not identical";
} else {
    echo "Identical";
}
?>
Drag options to blanks, or click blank then click option'
A!=
B!==
C==
D===
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of !== causes loose inequality check.
4fill in blank
hard

Fill both blanks to create a dictionary that maps comparison operators to their descriptions.

PHP
<?php
$comparisons = [
    '[1]' => 'Loose equality',
    '[2]' => 'Strict equality'
];
print_r($comparisons);
?>
Drag options to blanks, or click blank then click option'
A==
B===
C!=
D!==
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up operators or using inequality operators.
5fill in blank
hard

Fill all three blanks to complete the code that checks different comparisons and prints results.

PHP
<?php
$a = '0';
$b = 0;
if ($a [1] $b) {
    echo "Loose equal\n";
}
if ($a [2] $b) {
    echo "Strict equal\n";
}
if ($a [3] $b) {
    echo "Not strict equal\n";
}
?>
Drag options to blanks, or click blank then click option'
A==
B===
C!==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing != with !== or mixing up equality operators.