How to use strict types with bool in PHP

1 Answer

0 votes
declare(strict_types=1);

function flag(bool $b): void {
    var_dump($b);
}

flag(true); // OK
flag(1); // Fatal error: Uncaught TypeError: flag(): Argument #1 ($b) must be of type bool, int given
flag("true"); // Fatal error: Uncaught TypeError: flag(): Argument #1 ($b) must be of type bool, string given



/*
run:

ERROR!
bool(true)

Fatal error: Uncaught TypeError: flag(): Argument #1 ($b) must be of type bool, int given

*/

 



answered Aug 9 by avibootz
...