How to uses strict types reject float - int coercion in PHP

1 Answer

0 votes
declare(strict_types=1);

function half(int $n): float {
    return $n / 2;
}

echo half(10);   // OK
echo half(3.14); // TypeError: float passed to int parameter


/*
run:

5
Fatal error: Uncaught TypeError: half(): Argument #1 ($n) must be of type int, float given,

*/

 



answered Aug 8 by avibootz
...