To declare a return type for the function, add a colon (🙂 followed by the type just before the opening curly brace ({) when defining the function.
In the following example, we specify the return type for the function:
<?php declare(strict_types=1); // strict requirement function addNumbers(float $a, float $b) : float { return $a + $b; }
echo addNumbers(1.2, 5.2); ?>
|
You can specify a return type that differs from the argument types, but ensure that the return value is of the correct type.
<?php declare(strict_types=1); // strict requirement function addNumbers(float $a, float $b) : int { return (int)($a + $b); }
echo addNumbers(1.2, 5.2); |