Slide 109
Slide 109 text
declare(strict_types=1);
/ /
using union type to annotate a function parameter
function power(float|int $number, int $exponent)
:
int|float {
return $number
* *
$exponent;
}
echo power(3, 2);
/ /
9
echo power(3.5, 2);
/ /
12.25
echo power('3', 2.5);
/ /
Uncaught TypeError: power()
:
Argument #1
($number) must be of type int|float, string given