Challenge - 5 Problems
Union Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of function with union type parameter
What is the output of this PHP code using union types?
PHP
<?php function describe(mixed $value): string { if (is_int($value)) { return "Integer: $value"; } elseif (is_string($value)) { return "String: $value"; } else { return "Other type"; } } // Call with union type int|string function show(int|string $input): string { return describe($input); } echo show(10) . "\n"; echo show("hello") . "\n"; echo show(3.14) . "\n";
Attempts:
2 left
💡 Hint
Remember union types restrict accepted types. The third call uses a float which is not allowed.
✗ Incorrect
The function show() accepts only int or string. The first two calls pass valid types and return expected strings. The third call passes a float, which is not allowed by the union type, causing a TypeError. However, since the code calls describe() with mixed, it returns 'Other type' for non-int/string values. But show() restricts input to int|string, so passing float causes a TypeError before describe() runs.
❓ Predict Output
intermediate2:00remaining
Return type union behavior
What will this PHP code output when run?
PHP
<?php function getValue(bool $flag): int|string { if ($flag) { return 42; } else { return "forty-two"; } } var_dump(getValue(true)); var_dump(getValue(false));
Attempts:
2 left
💡 Hint
The function returns either an int or a string depending on the boolean input.
✗ Incorrect
The function getValue declares a union return type int|string. When called with true, it returns the integer 42. When called with false, it returns the string 'forty-two'. Both are valid return types, so var_dump outputs the type and value accordingly.
🔧 Debug
advanced2:00remaining
Identify the error caused by union type mismatch
What error will this PHP code produce when executed?
PHP
<?php function process(int|string $data): void { echo $data; } process(100); process("test"); process(3.14);
Attempts:
2 left
💡 Hint
Check the type of the third argument passed to the function.
✗ Incorrect
The function process() accepts only int or string. The first two calls pass valid types. The third call passes a float (3.14), which is not allowed by the union type. This causes a TypeError at runtime, stopping execution with a fatal error.
📝 Syntax
advanced2:00remaining
Which union type declaration is valid?
Which of the following PHP function signatures correctly declares a union type parameter?
Attempts:
2 left
💡 Hint
Check the spacing and syntax around the union type declaration.
✗ Incorrect
Option A is correct syntax: no spaces around the pipe character. Option A has spaces around the pipe which is invalid syntax in PHP union types. Option A is invalid because null is not allowed without a question mark or explicit nullable type. Option A ends with a pipe which is a syntax error.
🚀 Application
expert3:00remaining
Determine the number of valid keys in union typed array
Given this PHP code, how many keys will the resulting array have?
PHP
<?php /** * @param array<int|string, int|string> $input * @return array<int|string, int|string> */ function transform(array $input): array { $result = []; foreach ($input as $key => $value) { if (is_int($key) && $key % 2 === 0) { $result[$key] = (string)$value; } elseif (is_string($key) && strlen($key) > 3) { $result[$key] = (int)$value; } } return $result; } $input = [ 2 => 100, 3 => 200, "name" => "1234", "id" => "5678", "longkey" => "42" ]; $output = transform($input); echo count($output);
Attempts:
2 left
💡 Hint
Check which keys meet the conditions inside the loop.
✗ Incorrect
The function adds to $result only keys that are even integers or strings longer than 3 characters. Keys 2 (even int), "name" (length 4), and "longkey" (length 7) qualify. Keys 3 (odd int) and "id" (length 2) do not. So the result has 3 keys.