HOWTO: define return type in the function definition

This forum covers all minor versions of PHP 7
Post Reply
User avatar
Administrator
Site Admin
Posts: 1725
Joined: Thu Mar 22, 2018 10:19 am

HOWTO: define return type in the function definition

Post by Administrator » Sat Mar 24, 2018 7:49 am

This feature is new to PHP 7 it will allow you to define a return type when you are defining a new function.

you can accomplish this by adding a colon after the function definition parameters closing parentheses then the type you wish to return

Code: Select all

function arrayToString(array $arrayOfStrings): string
{
    $collapse = implode(" ", $arrayOfStrings);
    
    return $collapse;
}

echo arrayToString(array("one", "two", "three", "four"));
The output would be

Code: Select all

one two three four

for more information visit

http://php.net/manual/en/migration70.new-features.php

Post Reply