Challenge - 5 Problems
Master of PHP Comparison
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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'; } ?>
Attempts:
2 left
💡 Hint
Loose comparison converts types before comparing.
✗ Incorrect
In PHP, the loose comparison operator (==) converts types if needed. Here, integer 0 and string '0' are considered equal.
❓ Predict Output
intermediate2: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'; } ?>
Attempts:
2 left
💡 Hint
Strict comparison checks both value and type.
✗ Incorrect
The strict comparison operator (===) checks both value and type. Integer 0 and string '0' are different types, so result is 'Not equal'.
❓ Predict Output
advanced2: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'; } ?>
Attempts:
2 left
💡 Hint
Loose comparison converts string to boolean when compared to boolean.
✗ Incorrect
When comparing boolean to string with loose comparison, the string is converted to boolean. Non-empty string converts to true, so false == true is false, resulting in 'Not equal'.
❓ Predict Output
advanced2: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'; } ?>
Attempts:
2 left
💡 Hint
Strict comparison requires same type and value.
✗ Incorrect
Strict comparison (===) checks type and value. Boolean false and string 'any string' differ in type, so result is 'Not equal'.
🧠 Conceptual
expert3:00remaining
Why does loose comparison between 0 and 'foo' return true?
In PHP, why does the expression (0 == 'foo') evaluate to true?
Attempts:
2 left
💡 Hint
Think about how PHP converts strings to numbers in loose comparison.
✗ Incorrect
In loose comparison, PHP converts the string 'foo' to 0 because it does not start with a numeric value. Then it compares 0 == 0, which is true.