Void

void ist eine reine Rückgabedeklaration, die anzeigt, dass die Funktion keinen Wert zurückgibt, aber trotzdem beendet werden kann. Daher kann es nicht Teil der Deklaration eines Union-Typs sein. Verfügbar seit PHP 7.1.0.

Hinweis: Auch wenn eine Funktion den Rückgabetyp void hat, gibt sie einen Wert zurück, und dieser Wert ist immer null.

add a note

User Contributed Notes 1 note

up
0
harl at gmail dot com
3 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