php7 updatesNo Comments

default thumbnail

In php7, return type can be declared in a function and make it a must to return the expected type. (strict_types=1)
As an example,


function multiply($x, $y): int {
return $x * $y
}

var_dump(multiply(2, 3))

Result – > int(3)

Following snippet try to return a floating value which will end up giving out a Fatal error.


declare(strict_types=1);
function multiply($x, $y): int {
return $x * $y
}

var_dump(multiply(2, 3.2))

Result – > Will produce a Fatal error: Uncaught TypeError: Return value of multiply() must be of the type integer, float returned ……

Be the first to post a comment.

Add a comment