0
0
PHPprogramming~20 mins

Loose comparison vs strict comparison in PHP - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of PHP Comparison
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of loose comparison with different types
What is the output of this PHP code snippet?
PHP
<?php
$a = 0;
$b = '0';
if ($a == $b) {
    echo 'Equal';
} else {
    echo 'Not equal';
}
?>
ANot equal
BError
CEqual
D0
Attempts:
2 left
💡 Hint
Loose comparison converts types before comparing.
Predict Output
intermediate
2:00remaining
Output of strict comparison with different types
What is the output of this PHP code snippet?
PHP
<?php
$a = 0;
$b = '0';
if ($a === $b) {
    echo 'Equal';
} else {
    echo 'Not equal';
}
?>
AEqual
BNot equal
CError
D0
Attempts:
2 left
💡 Hint
Strict comparison checks both value and type.
Predict Output
advanced
2:00remaining
Loose comparison with boolean and string
What is the output of this PHP code snippet?
PHP
<?php
$a = false;
$b = 'any string';
if ($a == $b) {
    echo 'Equal';
} else {
    echo 'Not equal';
}
?>
ANot equal
BError
CEqual
DFalse
Attempts:
2 left
💡 Hint
Loose comparison converts string to boolean when compared to boolean.
Predict Output
advanced
2:00remaining
Strict comparison with boolean and string
What is the output of this PHP code snippet?
PHP
<?php
$a = false;
$b = 'any string';
if ($a === $b) {
    echo 'Equal';
} else {
    echo 'Not equal';
}
?>
AEqual
BFalse
CError
DNot equal
Attempts:
2 left
💡 Hint
Strict comparison requires same type and value.
🧠 Conceptual
expert
3:00remaining
Why does loose comparison between 0 and 'foo' return true?
In PHP, why does the expression (0 == 'foo') evaluate to true?
ABecause 'foo' is converted to 0 in numeric context, so 0 == 0 is true.
BBecause PHP treats all strings as true in loose comparison.
CBecause PHP throws a warning and returns true by default.
DBecause 0 is converted to string '0' and compared to 'foo'.
Attempts:
2 left
💡 Hint
Think about how PHP converts strings to numbers in loose comparison.