A union type allows a variable or parameter to accept values of multiple specified types. It is written using the pipe symbol (|) between types.
Click to reveal answer
beginner
How do you declare a function parameter that accepts either an int or a string using union types?
You declare it like this: <code>function example(int|string $param) { }</code>. This means $param can be an int or a string.
Click to reveal answer
intermediate
Can union types include null in PHP? How?
Yes. You can include null in a union type like int|null. This means the value can be an int or null.
Click to reveal answer
intermediate
What happens if a value passed to a union typed parameter does not match any of the types?
PHP will throw a TypeError at runtime because the value does not match the declared union types.
Click to reveal answer
advanced
Can you use union types with class types in PHP?
Yes. You can declare a parameter or property to accept multiple class types like <code>ClassA|ClassB</code>. The value must be an instance of either class.
Click to reveal answer
Which symbol is used to declare union types in PHP?
A:
B&
C,
D|
✗ Incorrect
The pipe symbol (|) is used to separate types in a union type declaration.
What will happen if you pass a float to a parameter declared as int|string?
AIt will accept the float without error.
BIt will throw a TypeError.
CIt will convert the float to int automatically.
DIt will ignore the parameter.
✗ Incorrect
Passing a float to a parameter declared as int|string causes a TypeError because float is not allowed.
Can union types include the type 'null'?
AYes, by including null in the union.
BNo, null cannot be part of union types.
COnly if the type is nullable with ?.
DOnly for class types.
✗ Incorrect
You can include null explicitly in union types, like int|null.
Which PHP version introduced union types?
APHP 8.0
BPHP 7.4
CPHP 8.1
DPHP 7.2
✗ Incorrect
Union types were introduced in PHP 8.0.
Is this a valid union type declaration? function test(int|float|string $value) {}
ANo, float cannot be in union types.
BNo, only two types allowed in union.
CYes, it accepts int, float, or string.
DYes, but only in PHP 7.
✗ Incorrect
Union types can include multiple types separated by |, so this is valid.
Explain what union types are in PHP and how to use them in function parameters.
Think about allowing multiple types for one variable.
You got /3 concepts.
Describe what happens if a value does not match any type in a union type declaration.
Consider PHP's behavior when types don't match.
You got /3 concepts.
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.