Union types let you say a value can be one of several types. This helps your code accept different kinds of data safely.
When a function can accept more than one type of input, like a number or a string.
When a variable might hold different types at different times.
When you want to be clear about all possible types a value can have.
When you want to avoid errors by checking types before using values.
Syntax
PHP
function example(int|string $value): void {
// code here
}
Use the pipe symbol | to separate types.
Union types are supported in PHP 8.0 and later.
Examples
This function accepts either an integer or a string and prints it.
PHP
function printValue(int|string $value): void {
echo $value;
}
This function accepts a string or an array and returns its length.
PHP
function getLength(string|array $input): int {
return is_string($input) ? strlen($input) : count($input);
}
Using mixed is a broader type, but union types are more specific.
PHP
function process(mixed $data): void {
if (is_int($data) || is_string($data)) {
echo"Data is int or string: $data";
}
}
Sample Program
This program defines a function that accepts either an integer or a string. It checks the type and prints a message accordingly.
PHP
<?php
function describeValue(int|string $value): void {
if (is_int($value)) {
echo"Integer: $value\n";
} else {
echo"String: $value\n";
}
}
describeValue(10);
describeValue("hello");
OutputSuccess
Important Notes
Union types improve code clarity and reduce bugs by enforcing allowed types.
Nullable types like ?int can be written as int|null with union types.
Union types cannot contain duplicate types or void.
Summary
Union types let a value be one of several types using the | symbol.
They help functions and variables accept multiple types safely.
Supported from PHP 8.0 onwards.
Practice
(1/5)
1. What does a union type in PHP allow you to do? function example(int|string $value) {}
easy
A. Only accept integer values
B. Accept multiple types for a parameter or return value
C. Only accept string values
D. Combine two functions into one
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 an int or a string as input.
Final Answer:
Accept multiple types for a parameter or return value -> Option B
Quick Check:
Union types allow multiple types [OK]
Hint: Look for the | symbol to spot union types [OK]
Common Mistakes:
Thinking union types combine functions
Assuming only one type is allowed
Confusing union types with type casting
2. Which of the following is the correct syntax for a union type in PHP 8+? function test(?? $param) {}
easy
A. int|string, float $param
B. int&string $param
C. int|string&float $param
D. int|string $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.