[SR-Users] how can string "0" be equal to int 0?

Iñaki Baz Castillo ibc at aliax.net
Fri Oct 15 18:03:38 CEST 2010


2010/10/15 Iñaki Baz Castillo <ibc at aliax.net>:
> 1) Never do implicit type conversion. This is, a string is NEVER equal
> to an integer:
>
>    if "0" == 0     =>  false
>    if "" == 0       => false
>    if "asdasd" == 0     => false
>
>
> 2) An integer is NEVER equal to a string:
>
>    if 0 == "0"     =>  false
>    if 0 == ""       => false
>    if 0 == "asdasd"     => false
>
>
> 3) Integer 0 is not equal to NULL:
>
>     if 0 == NULL   => false
>
>
> 4) Empty string is not equal to NULL:
>
>     if "" == NULL  => false
>
>
> 5) Integer 0 is true:
>
>     if 0   => true
>
>
> 6) Empty string is true:
>
>     if ""  => true


These simple rules avoid stupid problems (as they occur in PHP):

  http://www.otton.org/2008/08/06/stupid-php-tricks-true-false-comparison/

-- 
Iñaki Baz Castillo
<ibc at aliax.net>



More information about the sr-users mailing list