0

I just var_dump(false==0) it outputs bool(true) Why false== 0 is true.I know true==1 is true Because if i echo true; It will output 1 so numeric value of true is 1, But numeric value of false is not 0, because when i echo false; It display nothing(empty), So how can false has same value as 0 AS we know == operator compares the values , if they same it will return true, and if their values is not same it will return false , so in the case of false==0 It should be false. Any idea ?

beginner
  • 1,325
  • 2
  • 19
  • 41

3 Answers3

1

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string values.

So both false == "" and false == 0 are true. Remember, "0" is not the same as 0.

Hatted Rooster
  • 33,170
  • 5
  • 52
  • 104
  • This doesn't answer my question , you are saying `false==0` is true , I know this, but i am asking why ? false value is converted into "" not into 0 – beginner Oct 08 '16 at 09:28
  • @beginner read what I wrote again. 0 is the same as "". Just a string or an int representation. – Hatted Rooster Oct 08 '16 at 10:22
1

You can have a check here.

PHP Type Comparison

In short, == is the loose comparison operator which invokes type conversion before comparison. Maybe you should use the strict comparison operator === instead.

The same story goes in JavaScript.

jules3c
  • 37
  • 2
0

false has the the same value as 0, it is just another way of writing it

So, false == 0 will be the same thing as saying

0 == 0 which returns true because 0 = 0

Aaron Garton
  • 556
  • 6
  • 15
  • I understand false value is just 0 , the conception is that false don't `echo false` dont prints out `0` so question is why `echo false` don't print 0 – beginner Oct 08 '16 at 14:40