ZetCode

PHP mixed Keyword

last modified November 25, 2026

The PHP mixed keyword is a pseudo-type introduced in PHP 8.0. It accepts every PHP type, including null, and documents that a parameter, return value, or property is intentionally unrestricted.

mixed is useful at the boundaries of a program, where data arrives from configuration, JSON, or a database. Inside the program, a precise type or a union is safer and easier to understand.

Basic definitions

The mixed pseudo-type, introduced in PHP 8.0, is the top type of PHP's type system: a value of type mixed may be an array, bool, callable, int, float, null, object, resource, or string. It can be used for parameters, return values, and properties.

mixed already includes null, so ?mixed is a fatal error. It must also stand alone: string|mixed is invalid because the union adds nothing to mixed. The same applies to intersection types and to combinations with void or never.

An untyped parameter also accepts any value, but mixed is a real declaration: it appears in reflection and in generated documentation, and it can be used as a property type. The type does not perform any runtime check beyond accepting every value; the function still has to narrow the value before using type-specific operations.

Use mixed at the boundary of code that genuinely accepts anything, such as a configuration reader, a serializer, or a debugging helper. Inside the function, convert the value to a precise type as early as possible, and prefer a union such as int|string when only a few types are allowed.

A mixed parameter

The first example accepts a value of any type and returns a short description. A match expression with type checks keeps the branches flat.

describe.php
<?php

declare(strict_types=1);

function describe(mixed $value): string
{
    return match (true) {
        is_int($value) => "int: $value",
        is_string($value) => "string: " . strtoupper($value),
        is_bool($value) => "bool: " . var_export($value, true),
        $value === null => "null",
        default => get_debug_type($value),
    };
}

echo describe(42), "\n";
echo describe("hello"), "\n";
echo describe(true), "\n";
echo describe(null), "\n";
echo describe([1, 2, 3]), "\n";

is_int, is_string, and is_bool narrow the value in each arm. The comparison $value === null handles null explicitly, and get_debug_type covers everything else, including arrays and objects. The return type of the function is string, so the caller gets a predictable result even though the input is unrestricted.

$ php describe.php
int: 42
string: HELLO
bool: true
null
array

A mixed return type

A configuration reader often returns values of different types: an integer timeout, a boolean flag, a string name, or an array of hosts. Declaring the return type as mixed documents that fact.

config.php
<?php

declare(strict_types=1);

function config(string $key): mixed
{
    $config = [
        'timeout' => 30,
        'debug' => true,
        'name' => 'App',
        'hosts' => ['a.example.com', 'b.example.com'],
    ];

    return $config[$key] ?? null;
}

var_dump(config('timeout'));
var_dump(config('debug'));
var_dump(config('name'));
var_dump(config('hosts'));
var_dump(config('missing'));

var_dump shows the actual type of each value, including NULL for the missing key. A union such as int|bool|string|array|null would be more precise, but it would have to be repeated in every signature and updated whenever the configuration gains a new value type. The flexible return type shifts the responsibility for checking to the caller.

$ php config.php
int(30)
bool(true)
string(3) "App"
array(2) {
  [0]=>
  string(13) "a.example.com"
  [1]=>
  string(13) "b.example.com"
}
NULL

Validating mixed input

Accepting mixed does not mean giving up validation. The function below checks that both arguments are numeric before performing arithmetic, and reports invalid input instead of producing a warning.

checks.php
<?php

declare(strict_types=1);

function add(mixed $a, mixed $b): float
{
    if (!is_numeric($a) || !is_numeric($b)) {
        echo "skipping non-numeric input\n";
        return NAN;
    }

    return (float) $a + (float) $b;
}

echo add(10, 5.5), "\n";
echo add("3", "2.5"), "\n";
var_dump(add("foo", 5));

is_numeric accepts integers, floats, and numeric strings, so the two valid calls produce 15.5 and 5.5. The invalid call returns NAN (not a number) after printing a message; var_dump shows it as float(NAN). Returning a special value is only one option: a production function would usually throw an InvalidArgumentException or return a result object.

$ php checks.php
15.5
5.5
skipping non-numeric input
float(NAN)

Mixed properties

A property declared as mixed can hold different types at different times. This is useful for a settings bag, but it removes the guarantees that a typed property provides.

properties.php
<?php

declare(strict_types=1);

class UserSettings
{
    public mixed $theme = 'light';
    public mixed $lastLogin;

    public function setPreference(mixed $value): void
    {
        $this->theme = $value;
    }

    public function getTheme(): mixed
    {
        return $this->theme;
    }
}

$settings = new UserSettings();
$settings->setPreference(42);
$settings->lastLogin = new DateTimeImmutable('2026-11-25 10:00');

var_dump($settings->getTheme());
echo $settings->lastLogin->format('Y-m-d H:i'), "\n";

The theme starts as the string light and is then replaced by the integer 42. lastLogin holds a DateTimeImmutable. The property is still typed: assigning any value is allowed, but reading it before initialization raises an Error, just like any other typed property without a default. Prefer a union or a small value object when the set of possible types is known.

$ php properties.php
int(42)
2026-11-25 10:00

Mixed and callables

A mixed value may contain a callable: a closure, a function name, or an array of an object and a method name. Check it with is_callable before calling it.

callable.php
<?php

declare(strict_types=1);

class Formatter
{
    public function format(string $value): string
    {
        return "[" . strtoupper($value) . "]";
    }
}

function apply(mixed $callback, mixed $value): mixed
{
    if (!is_callable($callback)) {
        return 'not callable';
    }

    return $callback($value);
}

echo apply(fn($x) => $x * 2, 21), "\n";
echo apply('strtoupper', 'hello'), "\n";
echo apply([new Formatter(), 'format'], 'world'), "\n";
echo apply(42, 'hello'), "\n";

The arrow function doubles 21, the function name strtoupper uppercases a string, and the array [new Formatter(), 'format'] calls an instance method. The integer 42 is not callable, so the function returns a message instead of triggering an error. In a real API, return a typed result or throw an exception rather than mixing a string error into the normal return value.

$ php callable.php
42
HELLO
[WORLD]
not callable

Mixed cannot be combined

Because mixed already contains every type, it cannot be part of a union. The following declaration is rejected when the file is compiled, before any code runs.

union_error.php
<?php

declare(strict_types=1);

function invalid(string|mixed $value): void
{
    echo $value;
}

invalid("test");

The error message is Type mixed can only be used as a standalone type. The same restriction applies to ?mixed, which reports that mixed already includes null. If only some types are acceptable, list them in a union instead of adding mixed to one.

$ php union_error.php
Fatal error: Type mixed can only be used as a standalone type in /app/union_error.php on line 5
Stack trace:
#0 {main}

Mixed and null

null is a normal member of the mixed type. A function that accepts mixed must decide whether null is valid and handle it explicitly.

null.php
<?php

declare(strict_types=1);

function handle(mixed $input): string
{
    if ($input === null) {
        return 'null';
    }

    if ($input === false) {
        return 'false';
    }

    if ($input === '') {
        return 'empty string';
    }

    if (is_array($input)) {
        return 'array: ' . count($input) . ' items';
    }

    return get_debug_type($input) . ': ' . var_export($input, true);
}

foreach ([null, false, '', 0, [], 'text'] as $value) {
    echo handle($value), "\n";
}

The function treats null, false, and the empty string as special cases, then describes the remaining values. Note that 0 is not treated as empty here, and an empty array is reported as an array with zero items. These distinctions are easy to lose with a loose check such as if (!$input).

$ php null.php
null
false
empty string
int: 0
array: 0 items
string: 'text'

Best practices

Use mixed as a last resort at the edges of a system, then narrow the value before the rest of the code sees it.

Source

PHP manual: mixed PHP manual: type declarations PHP manual: is_numeric PHP manual: get_debug_type

In this article we have used the PHP mixed type for parameters, return values, and properties, validated mixed input, checked callables, and seen why mixed cannot be combined with other types. See also the PHP mixed type tutorial, which covers the type in more depth, and the union types and iterable keyword articles.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all PHP tutorials.