Challenge - 5 Problems
Nullable Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function with nullable int parameter
What is the output of this PHP code when calling
describeAge(null);?PHP
<?php function describeAge(?int $age): string { if ($age === null) { return "Age is unknown."; } return "Age is $age."; } echo describeAge(null);
Attempts:
2 left
💡 Hint
Remember that the question mark before int means the parameter can be null.
✗ Incorrect
The function accepts an int or null. When null is passed, it returns 'Age is unknown.' because of the explicit check.
❓ Predict Output
intermediate2:00remaining
Return type with nullable string
What will this PHP code output when calling
getName(null);?PHP
<?php function getName(?string $name): ?string { if ($name === null) { return null; } return strtoupper($name); } var_dump(getName(null));
Attempts:
2 left
💡 Hint
The function returns null explicitly if input is null.
✗ Incorrect
The function returns null when input is null, so var_dump outputs NULL without quotes.
❓ Predict Output
advanced2:00remaining
Nullable type with default value
What is the output of this PHP code when calling
greet();?PHP
<?php function greet(?string $name = null): string { return $name ? "Hello, $name!" : "Hello, guest!"; } echo greet();
Attempts:
2 left
💡 Hint
The default value is null and the function checks if $name is truthy.
✗ Incorrect
Since no argument is passed, $name is null, which is falsy, so it returns 'Hello, guest!'.
❓ Predict Output
advanced2:00remaining
Nullable type with strict type enforcement
What happens when this PHP code runs?
PHP
<?php declare(strict_types=1); function multiply(?int $a, int $b): int { return ($a ?? 1) * $b; } echo multiply(null, 5);
Attempts:
2 left
💡 Hint
The null coalescing operator replaces null with 1 before multiplication.
✗ Incorrect
Since $a is null, ($a ?? 1) becomes 1, so 1 * 5 = 5 is returned and printed.
❓ Predict Output
expert3:00remaining
Nullable type with union and default
What is the output of this PHP code?
PHP
<?php function format(?string|int $value = null): string { return match (true) { is_int($value) => "Number: $value", is_string($value) => "Text: $value", default => "No value", }; } echo format();
Attempts:
2 left
💡 Hint
The default parameter is null, which matches the default case in match.
✗ Incorrect
Since $value is null, none of the first two conditions match, so 'No value' is returned.