Skip to main content
The void keyword in PHP is a return type declaration introduced in PHP 7.1 that explicitly indicates a function or method does not return a value to its caller. When a function is typed with void, the PHP engine enforces strict rules regarding the return statement within that function’s local scope.

Syntactic Rules and Behavior

Permitted Return Mechanisms A function declared with a void return type is restricted to two permitted return mechanisms (though execution may also terminate by throwing an Exception or halting the script via exit() or die()):
  1. Implicit return: Reaching the end of the function block without encountering a return statement.
  2. Explicit empty return: Using the return; statement without an accompanying expression.
Prohibited Return Statements Attempting to return any value from a void function results in a Fatal error: A void function must not return a value. This strictness extends to null. While a standard PHP function without a return type implicitly evaluates to null, a void function explicitly forbids the statement return null;.

Type System Restrictions

  • Scope of Usage: The void type is exclusively a return type. It cannot be used as a parameter type declaration or a class property type declaration. Attempting to do so will result in a parse error.
  • Union and Intersection Types: void cannot be combined with other types. Declarations such as void|string or ?void are syntactically invalid. The void type inherently represents the absence of a value, making it mutually exclusive with any type that represents a value.

Inheritance and Variance

In object-oriented PHP, return types are covariant. When a parent class method declares a void return type, an overriding method in a child class is strictly limited in how it can alter this signature:
  • The child method must declare a void return type, or (as of PHP 8.1) it may declare a never return type. Because never is a bottom type in PHP’s type system, it is a valid subtype of void.
  • A child class cannot widen the return type to omit void, nor can it change it to a specific value-bearing type like mixed, int, or null.
Tired of Poor PHP Skills? Fix That With Deep Grasping!Learn More