Function return type in PHP 7 and void return type in PHP 7.1

One of the beautifull feature of programming languages came out in PHP 7 that now we also can have function return type in PHP like we have in other languages like Java, C, C# etc..
So PHP 7 adds support for return type declarations.
Now we can force a function to return a specific type of data.


In PHP 7 we have following function return types:
int : Integer data type.

float : Float data type.

array : Array data type.

bool : Boolean data type.

string: String data type.

Class/Interface name : Name of the class.

self : Instance of the same class. This can only be used if function is created under any class.

callable : Valid PHP callable.


In PHP 7.1 void return type has been introduced. Functions declared with return type void can use an empty return statement. Keep in mind you also can't return null if you are declaring function with return type void.

void: return nothing.

 

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *