0
0
PHPprogramming~20 mins

Union types in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Union Types Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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";
A
Integer: 10
String: hello
Fatal error: Uncaught TypeError
B
Fatal error: Uncaught TypeError
String: hello
Other type
C
Integer: 10
String: hello
String: 3.14
D
Integer: 10
String: hello
Other type
Attempts:
2 left
💡 Hint
Remember union types restrict accepted types. The third call uses a float which is not allowed.
Predict Output
intermediate
2: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));
A
string(2) "42"
string(9) "forty-two"
B
int(42)
int(0)
CFatal error: Return type must be int|string
D
int(42)
string(9) "forty-two"
Attempts:
2 left
💡 Hint
The function returns either an int or a string depending on the boolean input.
🔧 Debug
advanced
2: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);
AParse error: syntax error, unexpected '3.14' (float literal)
BFatal error: Uncaught TypeError: Argument 1 passed to process() must be int|string, float given
C
Output:
100
test
3.14
DWarning: echo expects parameter 1 to be string, float given
Attempts:
2 left
💡 Hint
Check the type of the third argument passed to the function.
📝 Syntax
advanced
2:00remaining
Which union type declaration is valid?
Which of the following PHP function signatures correctly declares a union type parameter?
Afunction example(int|string $param): void {}
Bfunction example(int | string $param): void {}
Cfunction example(int |string|null $param): void {}
Dfunction example(int|string| $param): void {}
Attempts:
2 left
💡 Hint
Check the spacing and syntax around the union type declaration.
🚀 Application
expert
3: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);
A2
B4
C3
D5
Attempts:
2 left
💡 Hint
Check which keys meet the conditions inside the loop.