Return type declarations
Return type declarations specify the type of the value that will be returned from a function.
<?php
// Strict mode
declare(strict_types = 1);
function returnIntegerVal(int $val) : int {
return $val ; ;
}
print(returnIntegerVal(25));
?>
Below the example of Invalid Return Type:
<?php
// Strict mode
declare(strict_types = 1);
function returnIntegerVal(int $val) : int {
return $val + 2.5;
}
print(returnIntegerVal(25));
?>
Output:
Uncaught TypeError: Return value of returnIntegerVal() must be of the type integer, float returned in [...][...]:5