

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Union Type in PHP 8
- Finding average in mixed data type array in JavaScript
- Attributes in PHP 8
- 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
- Constructor Property Promotion in PHP 8
- PHP Type Operators
- PHP Type Juggling
- Difference between gettype() in PHP and get_debug_type() in PHP 8
- What is Stringable Interface in PHP 8?
- str_starts_with and str_ends_with function in PHP 8
- Type Hinting in PHP 7