Is there a difference between $ var and (bool) $ var?

Are there any differences between the operators !! or (bool) if you want to use data types?

  $number = 1; var_dump( (bool) $number ) //true var_dump( !! $number ) //true 
+6
source share
2 answers

(bool) , as pointed out in meagar in the comments, is more semantically meaningful. Also keep in mind that (bool) performs one operation as well !! performs two (double negation). The difference in performance is almost not detected, but can be added when used in bulk.

+7
source

Using '!!' makes you doubly deny the offer. How do you use! for this, PHP will automatically add the variable to boolean, and since you negate the negation, the value of the variable will remain unchanged. So casting a variable using '(bool)' or '!!' basically has the same effect.

0
source

Source: https://habr.com/ru/post/954290/


All Articles