
- 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
Mixed Pseudo Type in PHP 8
The mixed type in PHP 8 is a new built-in union type. Mixed type is equivalent to array|bool|callable|int|float. Mixing the type is not quite similar to omitting the type completely.
That means, the programmer just forgot to write it.
Sometimes the programmer prefers to omit some specific type to keep the compatibility with an older version.
Mixed type in PHP 8 can take any type of property/return/parameter. We can say that it includes null, callable, resource, all class objects, or all scalar types in PHP. The mixed type is equivalent to the Union type.
int|float|bool|string|null|array|object|callable|resource
Example: Mixed Type in PHP 8
<?php class Student{ public mixed $studentProperty; public function emp(mixed $emp): mixed {} } ?>
In PHP 8, Mixed is a pseudo/virtual type. It represents several types that PHP can handle, which means we cannot cast a variable to mixed because it does not make any logic.
$foo = (mixed) $bar;
Note: gettype() and get_debug_type() function can never return mixed as the type of a variable also.
We cannot use mixed in union with other types.
function(mixed|FooClass $bar): int|mixed {}
Note: In the above code, both union types are not allowed and It will give a fatal error.
Output
Fatal error: Type mixed can only be used as a standalone type in C:\xampp\htdocs\gud.php on line 2
Example: PHP 8 Program code using Mixed Type
<?php function debug_function(mixed ...$data){ print_r($data); } debug_function(10, 'string', []); ?>
Output
Array ( [0] => 10 [1] => string [2] => Array ( ) )
- Related Articles
- Union Type in PHP 8
- Finding average in mixed data type array in JavaScript
- 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
