[PHP-NOTES] note 130486 added to language.types.void

[email protected] ("[email protected]") Wed, 17 Sep 2025 00:38:18 +0000
Newsgroups php.notes
Message-ID <[email protected]>
"Even if a function has a return type of void it will still return a value, this value is always null."

Syntactically, a void-returning function's return statements must be plain `return;`. If the function is declared as returning null then the return statement needs to include a return value: `return null;`.

A function that always returns null ("function foo(): null {...}") is equivalent to a void-returning function ("function foo(): void {...}"). Neither actually returns any information to the caller (the caller always receives null, and doesn't learn anything from it).

Meaningful return type declarations using null are always union types. "function foo(): null" is legal but pointless and might as well be written as returning void to make explicit the fact that it doesn't return anything meaningful (and indicates that "$v = foo();" is certainly a mistake of some sort.) The exception to this is if foo() is a subclass method overriding a wider-but-still-nullable type from a superclass.

(And the technical reason why a void function still returns null is that a function call is an expression so needs to have SOME value if it returns at all.)
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.137 (proxied: 122.57.11.211)
----
Manual Page -- https://php.net/manual/en/language.types.void.php
Edit        -- https://main.php.net/note/edit/130486
Del: integrated  -- https://main.php.net/note/delete/130486/integrated
Del: useless     -- https://main.php.net/note/delete/130486/useless
Del: bad code    -- https://main.php.net/note/delete/130486/bad+code
Del: spam        -- https://main.php.net/note/delete/130486/spam
Del: non-english -- https://main.php.net/note/delete/130486/non-english
Del: in docs     -- https://main.php.net/note/delete/130486/in+docs
Del: other reasons-- https://main.php.net/note/delete/130486
Reject      -- https://main.php.net/note/reject/130486
Search      -- https://main.php.net/manage/user-notes.php