Can someone explain me why this:
var_dump((bool) 1==2);
returns
bool(true)
but
var_dump(1==2);
returns
bool(false)
Of course the second return is correct, but why in the first occasion php returns an unexpected value?
Can someone explain me why this:
var_dump((bool) 1==2);
returns
bool(true)
but
var_dump(1==2);
returns
bool(false)
Of course the second return is correct, but why in the first occasion php returns an unexpected value?
It's actually not as strange it seems. (bool)
has higher precedence than ==
, so this:
var_dump((bool) 1==2);
is equivalent to this:
var_dump( ((bool) 1) == 2);
or this:
var_dump(true == 2);
Due to type juggling, the 2
also essentially gets cast to bool
(since this is a "loose comparison"), so it's equivalent to this:
var_dump(true == true);
or this:
var_dump(true);