Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Types and Type Handling

PHP mixed, never and void types

Reference for the special PHP types mixed, void, never, static and null/true/false: what each means, where it is allowed and how they differ.

Besides ordinary types, PHP has a few special ones that describe what a function accepts or returns in unusual cases.

mixed means "any value", including null. void is a return type for functions that return nothing. never is a return type for functions that never come back: they always throw an exception or end the script. static (a return type only) means "an instance of the class the method was called on".

PHP
function log_message(string $text): void { echo $text; }
function fail(string $why): never { throw new RuntimeException($why); }
function pick(mixed $anything): mixed { return $anything; }

Try it

PHP mixed, void and never

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP static, true, false and null return types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
function f(mixed $x): mixed { }
function g(): void { }          // no value; "return;" is allowed
function h(): never { }          // throws or exits
function i(): static { }         // methods only
function j(): true { }           // PHP 8.2; also false and null
Type Meaning Allowed as
mixed Any value, including null parameter, return, property
void Returns nothing return type only
never Never returns normally return type only
static The late-bound class return type only
true, false, null That single value any position since 8.2 (false only in a union before)

Usage notes

  • mixed is explicit. An untyped parameter also accepts anything, but mixed documents that you really mean it and the engine checks that you did not forget the type.
  • mixed already includes null, so ?mixed and mixed|null are errors.
  • void returns null to the caller, but you cannot write return null; or return $x; in a void function, and you cannot use void in a union or as a parameter type.
  • never replaces void for exit paths. A function declared never must throw, call exit(), or loop forever; falling off the end throws a TypeError. Static analysers use this to know the code after the call is unreachable.
  • static return types are great for fluent builders, as in the second example. self would return the parent type for subclass calls.
  • Prefer precise types. mixed loses all the help from the type checker. Reach for unions (int|string) or interfaces first.
  • Version notes: void 7.1, mixed 8.0, static return 8.0, never 8.1, standalone null, true and false 8.2.