Luntegg
Luntegg

Reputation: 2556

Why condition is triggered?

$text = 'OMNOMNOM';

if($text == intval($text))
    echo 'yes';
else
    echo 'no';

Why condition is triggered, and we see "yes"? Why 'OMNOMNOM' == 0? What's the catch?

UPD:

If I write if('qwe' == 1), conditional return false, and if I write if('qwe' == 0), conditional return true... Why condition checked in integer, not in string?

Upvotes: 1

Views: 61

Answers (1)

Frank W
Frank W

Reputation: 891

Because by using == PHP tries to cast the values to the same type (in this case it seems like both as integers), if you use === then it will not cast and will care about the types (so to get true the type and value needs to be equal).

So try to use:

if($text === intval($text))
    echo 'yes';
else
    echo 'no';

Upvotes: 4

Related Questions