void Return Type — Functions That Return Nothing
A function with no return type could mean two very different things: it returns something and nobody bothered to declare it, or it genuinely returns nothing. PHP 7.1 let you say the second one out loud.
// Ambiguous: does this return the user? A bool? Nothing?
function save(User $user) {
$this->db->persist($user);
}
// Unambiguous, and enforced.
function save(User $user): void {
$this->db->persist($user);
}
void is stricter than it looks. You may write a bare return; to exit early, but returning an actual value is a compile-time error, not a runtime surprise.
function process(array $items): void {
if ($items === []) {
return; // fine: bare return exits early
}
foreach ($items as $item) {
$this->handle($item);
}
return true; // Fatal error: A void function must not return a value
}
// And calling code cannot pretend otherwise.
$result = process($items); // $result is null, and static analysis flags the assignment
The payoff is at the call site. : void tells every reader, IDE, and static analyzer that using the return value is meaningless, so PHPStan and Psalm can flag $result = save($user) as a bug rather than shrugging at it. It also documents intent: this function exists for its side effect, and that is the whole point of it.
Significance: Type Safety
Types are as much about what cannot happen as what can.
void was the first PHP return type that described an absence, and it started the pattern that never finished in 8.1: the type system describing control flow, not just data. Together they let analyzers reason about which code is reachable and which return values are real.