Challenge - 5 Problems
PHP Dynamic Typing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of adding string and integer
What is the output of this PHP code?
$a = "5 apples";
$b = 3;
echo $a + $b;
PHP
$a = "5 apples"; $b = 3; echo $a + $b;
Attempts:
2 left
💡 Hint
PHP converts strings to numbers when using arithmetic operators.
✗ Incorrect
PHP converts the string "5 apples" to the number 5 when used with +. So 5 + 3 = 8.
❓ Predict Output
intermediate2:00remaining
Boolean comparison with string
What is the output of this PHP code?
$x = "0";
if ($x == false) { echo "Yes"; } else { echo "No"; }
PHP
$x = "0"; if ($x == false) { echo "Yes"; } else { echo "No"; }
Attempts:
2 left
💡 Hint
In PHP, the string "0" is considered false in loose comparisons.
✗ Incorrect
When comparing with ==, "0" is treated as false, so the condition is true and "Yes" is printed.
🔧 Debug
advanced2:00remaining
Why does this string concatenation fail?
Consider this PHP code:
What error or output will this produce?
$a = 5;
$b = " apples";
echo $a - $b;
What error or output will this produce?
PHP
$a = 5; $b = " apples"; echo $a - $b;
Attempts:
2 left
💡 Hint
PHP tries to convert strings to numbers in arithmetic but " apples" has no leading digits.
✗ Incorrect
PHP issues a notice about non-numeric value because " apples" cannot be converted to a number, so it treats it as 0 and outputs 5 - 0 = 5 but with a notice.
❓ Predict Output
advanced2:00remaining
Result of loose equality with different types
What is the output of this PHP code?
$a = 0;
$b = "any string";
var_dump($a == $b);
PHP
$a = 0; $b = "any string"; var_dump($a == $b);
Attempts:
2 left
💡 Hint
When comparing number and string, PHP converts string to number. Non-numeric strings become 0.
✗ Incorrect
The string "any string" converts to 0, so 0 == 0 is true, var_dump prints bool(true).
🧠 Conceptual
expert3:00remaining
Understanding PHP type juggling in array keys
Given this PHP code:
What is the output and why?
$arr = [];
$arr["1"] = "one";
$arr[1] = "ONE";
echo count($arr);
What is the output and why?
PHP
$arr = []; $arr["1"] = "one"; $arr[1] = "ONE"; echo count($arr);
Attempts:
2 left
💡 Hint
PHP converts string keys that are numeric to integers in arrays.
✗ Incorrect
PHP treats "1" and 1 as the same key in arrays, so the second assignment overwrites the first. The array has one element.