php -a
Interactive shell
php > var_dump("some" == 0);
bool(true)
php > var_dump("some" == false);
bool(false)
php > var_dump("some" == true);
bool(true)
php > var_dump(false == 0);
bool(true)
My question is simple. What is the reasoning behind ("some" == 0) === true
? I understand that due to the weak typing the operands are converted to something comparable. But in this particular example it's against how I understand it.
If the zero would be converted to string it would be:
php > var_dump("some" == "0");
bool(false)
If it was converted to bool, it would be:
php > var_dump("some" == false);
bool(false)
Only sensible explanation is:
php > var_dump(intval("some"));
int(0)
php > var_dump(intval("some") == 0);
bool(true)
But that's certainly not what I or anyone would expect. Especially because any string would be turned to zero (and that potentially means false in any comparison).
Why is it this way?
Update:
My question was more about "why" not "if" or "how", sorry if that was not quite obvious. And I think I found my answer in Why PHP casts two numerical strings to numbers before [loosely] comparing them?