I would like to know if casting an integer to a boolean in any of these ways is a good idea.
I'm using PHP 7.4
Negating the integer twice (Which seems not optimal to me)
$x = 123;
$bool = !!($x);
Or when I pass the integer as a param and tell it to receive a boolean.
In this case PHP converts the integer to a boolean automatically
negative numbers => true
0 => false
positive numbers => true
function abc(bool $required = true) {
var_dump($required);
}
abc(-123); // bool(true)
abc(0); // bool(false)
abc(123); // bool(true)