Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between !!$var and (bool) $var?

Tags:

php

casting

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
like image 944
Aristona Avatar asked Sep 20 '13 19:09

Aristona


People also ask

What is the difference between $VAR and $$ VAR?

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.

Is there a difference between bool and Boolean?

bool is an alias for System. Boolean just as int is an alias for System.

Is bool and Boolean same in C#?

There is NO difference whatsoever. You may use either bool or Boolean with exactly the same results.

How do you check if a bool is true in PHP?

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.


1 Answers

(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.

like image 130
theftprevention Avatar answered Sep 21 '22 05:09

theftprevention