Union types in PHP - Time & Space Complexity
Let's explore how using union types in PHP affects the time it takes for code to run.
We want to see how the program's work changes as input grows when union types are involved.
Analyze the time complexity of the following code snippet.
function processValue(int|string $value): string {
if (is_int($value)) {
return (string)($value * 2);
} else {
return strtoupper($value);
}
}
$inputs = [1, "hello", 3, "world"];
foreach ($inputs as $item) {
echo processValue($item) . "\n";
}
This code processes a list of values that can be either numbers or words, using union types to accept both.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each item in the input array and calling
processValue. - How many times: Once for each item in the input list.
As the number of items grows, the code runs the process for each item one by one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 calls to processValue |
| 100 | About 100 calls to processValue |
| 1000 | About 1000 calls to processValue |
Pattern observation: The work grows directly with the number of items; doubling items doubles work.
Time Complexity: O(n)
This means the time to finish grows in a straight line with the number of inputs.
[X] Wrong: "Using union types makes the code slower because it checks types every time."
[OK] Correct: The type check is simple and happens once per item, so it doesn't change how the total work grows with more inputs.
Understanding how union types affect performance helps you write clear code without worrying about slowing things down as data grows.
"What if the input array was nested arrays instead of simple values? How would the time complexity change?"
Practice
function example(int|string $value) {}Solution
Step 1: Understand union type syntax
The|symbol between types means the parameter can be either type.Step 2: Analyze the example function
The function accepts either anintor astringas input.Final Answer:
Accept multiple types for a parameter or return value -> Option BQuick Check:
Union types allow multiple types [OK]
- Thinking union types combine functions
- Assuming only one type is allowed
- Confusing union types with type casting
function test(?? $param) {}Solution
Step 1: Recall union type syntax
Union types use the|symbol between types, e.g.,int|string.Step 2: Check each option
int|string $param uses|correctly. int&string $param uses&which is intersection, not union. int|string&float $param mixes union and intersection incorrectly. int|string, float $param uses a comma which is invalid.Final Answer:
int|string $param -> Option DQuick Check:
Union types use | symbol [OK]
- Using & instead of | for union types
- Separating types with commas
- Mixing intersection and union syntax incorrectly
function showType(int|string $value) {
if (is_int($value)) {
return "Integer: $value";
} else {
return "String: $value";
}
}
echo showType(10) . "\n";
echo showType("hello") . "\n";
Solution
Step 1: Understand the function behavior
The function checks if the input is an integer or string and returns a formatted string accordingly.Step 2: Trace the calls
Calling with 10 (int) returns "Integer: 10". Calling with "hello" (string) returns "String: hello".Final Answer:
Integer: 10\nString: hello -> Option CQuick Check:
Type check returns correct output [OK]
- Assuming output is just values without labels
- Confusing string and int outputs
- Expecting a type error for valid union types
function add(int|float $a, int|float $b): int|float {
return $a + $b;
}
add(5, "10");Solution
Step 1: Check parameter types
The function expects parameters of type int or float only.Step 2: Analyze the function call
The call passes 5 (int) and "10" (string). String is not allowed, causing a type error.Final Answer:
Passing a string "10" instead of int or float causes a type error -> Option AQuick Check:
Parameter types must match union types exactly [OK]
- Assuming string '10' is accepted as int
- Thinking union types disallow return types
- Ignoring parameter count
function sumValues(?? $input): int {
if (is_array($input)) {
return array_sum($input);
}
return $input;
}Solution
Step 1: Identify input types needed
The function accepts either a single integer or an array of integers.Step 2: Match union type to input
int|array $input usesint|array, which matches the requirement. Others include wrong types like string, float, or bool.Final Answer:
int|array $input -> Option AQuick Check:
Union type matches all accepted input types [OK]
- Using string or bool instead of array
- Not including all input types in union
- Confusing float with int
Ready to prove it?
Pass the test and this pattern moves unknown
