Special Types: callable, mixed, void, never, true and false

4.Special Types: callable, mixed, void, never, true and false

M

In this chapter

We'll learn the types that exist only in declarations — callable for functions passed as values, mixed for "anything", void for functions that return nothing, never for functions that never return, and the literal types true, false and null — so every function can say exactly what it does.

13–15 min

The Problem in Real Life

Marcus gives Mia a task: "Every function in pricing.php gets types. Parameters and return values. No exceptions." Most are quick: int $pricePence in, : int out.

Then she gets stuck. logBooking() writes a line to the log and gives nothing back — so what's its return type? applyRule() takes a whole function as its input — a discount rule. failBooking() prints an error and stops the file, so it never returns at all. And debugValue() accepts anything at all.

"Those four are why PHP has a few special type names," says Marcus. "They don't describe values you can store. They describe what a function does."

M

What's the return type of a function that doesn't return anything?

Mia

Types of Values vs. Types That Describe Behaviour

Nothing to return

Some functions do a job and give nothing back. Their return type still needs a name.

A function as input

A discount rule can be a function you pass to another function.

Functions that never come back

A function that always stops the file never returns to the caller.

Special Types: callable, mixed, void, never, true and false

Declaration-only types: the eight types in chapter 3 are types of values. PHP also has some type names that you use only in type declarations, to describe what a function accepts or what it does when it finishes:

  • callable: "something that can be called like a function".
  • mixed: "any type at all".
  • void: "this function returns nothing".
  • never: "this function never returns".
  • true, false and null: "exactly this one value".

callable: in PHP, a function can be passed to another function, just like a number. A callable is anything PHP can call as a function: the name of a function as a string ("abs"), a short arrow function written right where you need it (fn(int $p): int => $p - 500), and a few other forms you'll meet in Act 09. A parameter typed callable accepts any of them. Below, applyRule() takes a price and a discount rule, and calls the rule on the price. is_callable() checks whether something can be called.

callable: passing a function to a function
<?php
declare(strict_types=1);
function applyRule(int $pricePence, callable $rule): int
{
return $rule($pricePence); // call whatever was passed in
}
// An arrow function: £5 off
echo applyRule(2500, fn(int $p): int => $p - 500), "\n"; // 2000
// The name of a built-in function also works
echo applyRule(-300, "abs"), "\n"; // 300
var_dump(is_callable("strlen")); // bool(true)
var_dump(is_callable("noSuchThing")); // bool(false)

abs() gives a number without its minus sign. Act 09 covers arrow functions and callables fully.

mixed: mixed means "any type, including null". It's honest when a function really accepts anything — like a debugging helper that prints whatever you give it. But use it rarely: a mixed parameter tells PHP nothing, so PHP can't check anything for you. If you know the value is "an int or a string", say that instead — chapter 5 shows how.

mixed: a helper that accepts anything
<?php
declare(strict_types=1);
function debugValue(mixed $value): string
{
return get_debug_type($value);
}
echo debugValue(25), " ", debugValue("Tom"), " ", debugValue(null), "\n";
// int string null

void: void is the return type of a function that gives nothing back — it does a job, like writing a log line, and that's it. A void function may use a bare return; to stop early, but it must not return a value: return 5; in a void function is a fatal error before anything runs. If someone stores the result of a void function anyway, they get null, as below.

void: does a job, returns nothing
<?php
declare(strict_types=1);
function logBooking(string $customer): void
{
echo "Booked: $customer\n";
}
$result = logBooking("Zoë"); // Booked: Zoë
var_dump($result); // NULL
A void function must not return a value
<?php
declare(strict_types=1);
function logBooking(string $customer): void
{
return 5;
}
// Fatal error: A void function must not return a value in main.php on line 7

never: never is the return type of a function that never returns to the caller — because it always stops the file with exit, or always throws an error (Act 14). failBooking() is one: it prints a message and stops. Declaring never documents that the code after the call can never run, and PHP enforces it: if a never function reaches its end normally, PHP throws a TypeError.

never: a function that always stops the file
<?php
declare(strict_types=1);
function failBooking(string $reason): never
{
exit("Booking failed: $reason\n");
}
failBooking("no free slots"); // Booking failed: no free slots
echo "This line never runs";

true, false and null as types: since PHP 8.2, true, false and null can be used as types on their own, meaning "exactly this value". On their own they're rare. false is common inside a combination: many built-in functions return a result or false when they fail. strpos(), which finds where a piece of text starts, returns an int position, or false if the text isn't there. Its return type is written int|false — chapter 5 explains the |. The table below sums up all the special types.

Table — Declaration-only types at a glance
TypeWhere it's usedMeans
callableParametersSomething that can be called as a function
mixedParameters, returnsAny type, including null
voidReturn type onlyReturns nothing
neverReturn type onlyNever returns — always exits or throws
true / falseMostly in combinationsExactly that value, e.g. int|false
nullMostly in combinationsExactly null, e.g. string|null
A built-in function that returns int or false
<?php
var_dump(strpos("Tom's Barbershop", "Barber")); // int(6): found at position 6
var_dump(strpos("Tom's Barbershop", "Salon")); // bool(false): not found

Positions start at 0, so "Barber" starts at the 7th character, position 6.

Key Takeaway

Some types exist only in declarations: callable (anything that can be called — function names, arrow functions), mixed (any type — use sparingly), void (returns nothing), never (never returns: always exits or throws), and true/false/null (exactly that value, mostly used in combinations like int|false).

Why This Matters

With these few names, every function at BookBay can say exactly what it does — even the ones that return nothing or never return. void and never are documentation that PHP checks; callable makes flexible code like discount rules possible; and spotting mixed reminds a reviewer to ask "can we be more precise?".

And int|false is everywhere in PHP's own functions. Knowing that a function can return false instead of a result is how Mia will avoid a classic bug in Act 07, where position 0 is mistaken for "not found".

Every function in the pricing file now has types — except three. findNote() returns a string or null. formatPrice() should accept an int or a float. And the partner's amount might come as an int or a string. "One type isn't enough," says Mia. "Can a declaration say either?" That's the next chapter.