
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Union Type in PHP 8
Using Union Type in PHP 8, we can use the values of two or more types, instead of using a single type. To specify multiple types, vertical line (|) is used to join them.
Union type supports parameters, return types, and properties.
Syntax
type1|type2|……|type_n
Example 1: Union Type
<?php class Number { private int | float $num; public function getNum(): float | int { return $this->num } public function setNum(float | int): void { $this->num = $num; } } ?>
Example 2: PHP 8 Program using Union Type
<?php class Number{ private int|float $number; public function setNumber(int|float $number): void { $this->number = $number; } public function getNumber(): int|float { return $this->number; } #passing floats or integer values to the number object } $number = new Number(); $number->setNumber(5); print_r($number->getNumber()); $number->setNumber(11.54); print_r($number->getNumber()); ?>
Output
511.54
Nullable Types in Union Type
In PHP 7.1, nullable type is used with the question mark ?type. In PHP 8, we can declare nullable types as type|null. For example: float|int|null, but we cannot declare it as ?float|int.
Nullable Types Syntax
type1|type2|null
We should not declare like ?type1|type2 because this would be an ambiguous declaration.
Compile-time error checking − In PHP, union type duplicate or redundant types in a type declaration are not allowed. This check will happen during compile-time without autoloading classes/interfaces.
Duplicate types are not allowed − We cannot declare duplicates in PHP like int|int and also int|?int is not allowed. Because it will return a syntax error. "Fatal error: Duplicate type… is redundant in …on line."
void type is not allowed − In PHP 8, union type void is not allowed to combine with any other type.
Example
Function foo(): void|null{}
False Type in PHP 8
False type is a part of Union type in PHP 8. Mostly we use null as return values to show the absence of a return value or an error. For example: Suppose we have a class method that finds the student by the student ID. If Student ID is not present, it might give null to show that Student ID was not present. But PHP uses false if the Student ID is not found.
False type is used for any failure instead of null type.
We cannot use false as a standalone type and also nullable standalone types are not allowed. That means, we cannot use false, false|null, and ?false.
We can use false wherever types are accepted like a parameter, properties, and return values.
It has no true literal type, instead we can only use bool types.
Example
<?php Class Student { public function find(int $id): User|false { // ... } } ?>
- Related Articles
- Mixed Pseudo Type in PHP 8
- Attributes in PHP 8
- PHP Type Operators
- PHP Type Juggling
- Type Hinting in PHP 7
- Number comparisons in PHP 8
- Named Arguments in PHP 8
- Match Expression in PHP 8
- Nullsafe Operator in PHP 8
- fdiv() function in PHP 8
- PHP Boolean Data Type
- PHP Integer Data Type
- PHP String Data Type
- Difference between gettype() in PHP and get_debug_type() in PHP 8
- Difference between UNION and UNION ALL in DB2
