PHP Conference Fukuoka 2025

Void

void is a return-only type declaration indicating the function does not return a value, but the function may still terminate. Therefore, it cannot be part of a union type declaration. Available as of PHP 7.1.0.

Зауваження: Even if a function has a return type of void it will still return a value, this value is always null.

add a note

User Contributed Notes 1 note

up
1
harl at gmail dot com
19 days ago
"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.)
To Top