0
0
PHPprogramming~20 mins

PHP dynamic typing behavior - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Dynamic Typing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
A8
B53
C5 apples3
DError
Attempts:
2 left
💡 Hint
PHP converts strings to numbers when using arithmetic operators.
Predict Output
intermediate
2: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"; }
AYes
BError
C0
DNo
Attempts:
2 left
💡 Hint
In PHP, the string "0" is considered false in loose comparisons.
🔧 Debug
advanced
2:00remaining
Why does this string concatenation fail?
Consider this PHP code:
$a = 5;
$b = " apples";
echo $a - $b;

What error or output will this produce?
PHP
$a = 5;
$b = " apples";
echo $a - $b;
AFatal error
B5
C0
DNotice: A non-numeric value encountered
Attempts:
2 left
💡 Hint
PHP tries to convert strings to numbers in arithmetic but " apples" has no leading digits.
Predict Output
advanced
2: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);
Abool(false)
Bbool(null)
Cbool(true)
DError
Attempts:
2 left
💡 Hint
When comparing number and string, PHP converts string to number. Non-numeric strings become 0.
🧠 Conceptual
expert
3:00remaining
Understanding PHP type juggling in array keys
Given this PHP code:
$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);
A2 because string and integer keys are different
B1 because PHP treats string "1" and integer 1 as the same key
C0 because keys overwrite each other and array is empty
DError due to key type mismatch
Attempts:
2 left
💡 Hint
PHP converts string keys that are numeric to integers in arrays.