Is there any differences between using !!
or (bool)
operators when you want to cast data types?
$number = 1;
var_dump( (bool) $number ) //true
var_dump( !! $number ) //true
Difference between Both: The variable $var is used to store the value of the variable and the variable $$val is used to store the reference of the variable.
bool is an alias for System. Boolean just as int is an alias for System.
There is NO difference whatsoever. You may use either bool or Boolean with exactly the same results.
The is_bool() function checks whether a variable is a boolean or not. This function returns true (1) if the variable is a boolean, otherwise it returns false/nothing.
(bool)
, as mentioned by meagar in the comments, is more semantically meaningful. Also, keep in mind that (bool)
is performing one operation, while !!
is performing two (a double-negation). The difference in performance is pretty much undetectable, but it may add up if used in bulk.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With