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

1 Answer

0 votes
declare(strict_types=1);

function square(int $n): int {
    return $n * $n;
}

echo square(5);      // OK
echo square("5");    // TypeError: string is not allowed


/*
run:

25
Fatal error: Uncaught TypeError: square(): Argument #1 ($n) must be of type int, string given

*/

 



answered Aug 8 by avibootz
...