How to use strict types with arrays in PHP

1 Answer

0 votes
declare(strict_types=1);

function sum(array $numbers): int {
    return array_sum($numbers);
}

echo sum([1, 2, 3]);     // OK
echo sum("not array");   // TypeError



/*
run:

ERROR!
6
Fatal error: Uncaught TypeError: sum(): Argument #1 ($numbers) must be of type array, string given

*/

 



answered Aug 9 by avibootz
...