0
0
PHPprogramming~20 mins

Why type awareness matters in PHP - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Type Awareness Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this PHP code with type juggling?

Consider this PHP code snippet. What will it output?

PHP
<?php
$a = '5';
$b = 3;
echo $a + $b;
?>
AError: Unsupported operand types
B8
CTypeError
D'53'
Attempts:
2 left
💡 Hint

PHP automatically converts strings to numbers when using arithmetic operators.

Predict Output
intermediate
2:00remaining
What error occurs when adding incompatible types in PHP 8+?

What happens when you run this PHP code?

PHP
<?php
$a = 'hello';
$b = 10;
echo $a + $b;
?>
A0
B10
CTypeError
Dhello10
Attempts:
2 left
💡 Hint

PHP 8+ is stricter about adding strings that cannot convert to numbers.

🔧 Debug
advanced
2:00remaining
Why does this PHP function return unexpected results?

Look at this PHP function and its output. Why does it return 'Result: 15' instead of 'Result: 510'?

PHP
<?php
function add($x, $y) {
    return $x + $y;
}

echo 'Result: ' . add('5', '10');
?>
ABecause '+' concatenates strings, so it should output '510'.
BBecause the function is missing a return statement.
CBecause PHP throws a TypeError when adding strings.
DBecause '+' adds numbers, so strings are converted to integers before addition.
Attempts:
2 left
💡 Hint

Think about how PHP treats '+' with string operands.

📝 Syntax
advanced
2:00remaining
Which PHP code snippet correctly enforces strict typing?

Which option correctly enables strict typing and declares a function that only accepts integers?

A
&lt;?php
declare(strict_types=0);
function multiply(int $a, int $b): int {
    return $a * $b;
}
?&gt;
B
&lt;?php
function multiply(int $a, int $b): int {
    return $a * $b;
}
declare(strict_types=1);
?&gt;
C
&lt;?php
declare(strict_types=1);
function multiply(int $a, int $b): int {
    return $a * $b;
}
?&gt;
D
&lt;?php
strict_types=1;
function multiply(int $a, int $b): int {
    return $a * $b;
}
?&gt;
Attempts:
2 left
💡 Hint

Strict typing must be declared at the top of the file before any code.

🚀 Application
expert
3:00remaining
What is the output of this PHP code using union types and strict typing?

Given this PHP code, what will it output?

PHP
<?php
declare(strict_types=1);
function formatValue(int|string $value): string {
    return match(gettype($value)) {
        'integer' => "Number: $value",
        'string' => "Text: $value",
        default => 'Unknown',
    };
}

echo formatValue(42) . ', ' . formatValue('hello');
?>
ANumber: 42, Text: hello
B42, hello
CText: 42, Number: hello
DTypeError
Attempts:
2 left
💡 Hint

Look at how match uses gettype to decide the output string.