Challenge - 5 Problems
PHP Type Coercion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this PHP code with mixed types?
Consider the following PHP code snippet. What will it output?
PHP
<?php $a = '5'; $b = 3; $c = $a + $b; echo $c; ?>
Attempts:
2 left
💡 Hint
PHP converts strings to numbers when using arithmetic operators.
✗ Incorrect
In PHP, when adding a string containing a number to an integer, the string is converted to an integer. So '5' + 3 becomes 5 + 3 = 8.
❓ Predict Output
intermediate2:00remaining
What does this PHP code output when comparing string and integer?
What is the output of this PHP code snippet?
PHP
<?php var_dump('10' == 10); var_dump('10' === 10); ?>
Attempts:
2 left
💡 Hint
== compares values after type juggling, === compares both value and type.
✗ Incorrect
The == operator converts '10' to integer 10 and compares values, so true. The === operator checks type and value, string vs integer, so false.
❓ Predict Output
advanced2:00remaining
What is the output of this PHP code with boolean and string addition?
Analyze the following PHP code and select the correct output.
PHP
<?php $x = true + '2 apples'; echo $x; ?>
Attempts:
2 left
💡 Hint
PHP converts booleans to integers and strings to numbers when used with + operator.
✗ Incorrect
true converts to 1, '2 apples' converts to 2 (leading number), so 1 + 2 = 3.
❓ Predict Output
advanced2:00remaining
What error or output does this PHP code produce?
What happens when you run this PHP code?
PHP
<?php $result = 'hello' - 5; echo $result; ?>
Attempts:
2 left
💡 Hint
PHP converts non-numeric strings to 0 when used in arithmetic.
✗ Incorrect
'hello' converts to 0 because it does not start with a number, so 0 - 5 = -5.
❓ Predict Output
expert2:00remaining
What is the output of this PHP code with array and string addition?
Consider this PHP code. What will it output or error?
PHP
<?php $a = [1, 2]; $b = '3'; $c = $a + $b; echo gettype($c); ?>
Attempts:
2 left
💡 Hint
PHP does not support adding arrays and strings with + operator.
✗ Incorrect
Adding an array and a string with + operator causes a fatal error in PHP: Unsupported operand types.