Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
function identifier(): void {
    // execution block
}

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.
// Valid: Implicit return
function executeTask(): void {
    $operation = 2 + 2;
}

// Valid: Explicit empty return
function terminateEarly(bool $condition): void {
    if ($condition) {
        return; 
    }
    $operation = 2 + 2;
}
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;.
// Invalid: Triggers Fatal error
function returnString(): void {
    return "string"; 
}

// Invalid: Triggers Fatal error
function returnNull(): void {
    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.
class ParentClass {
    public function process(): void {}
}

class ChildClass extends ParentClass {
    // Valid: Exact match
    public function process(): void {}
}

class SubtypeChildClass extends ParentClass {
    // Valid (PHP 8.1+): 'never' is a bottom type and a valid subtype of 'void'
    public function process(): never {
        throw new RuntimeException('Halted');
    }
}

class InvalidChildClass extends ParentClass {
    // Invalid: Fatal error (Declaration must be compatible)
    public function process(): int {
        return 1;
    }
}
Master PHP with Deep Grasping Methodology!Learn More