-3

Possible Duplicate:
Why does PHP consider 0 to be equal to a string?
php string comparasion to 0 integer returns true?

it seems that as one has in PHP an if-statement where a function some_function() returns zero

<?php
if( some_function() == "whatever_you_want" ) { ... }

the statement will always be executed since

<?php
echo some_function() == "whatever_you_want";

is then TRUE.

Why behaves PHP in such a counter intuitive way?

Community
  • 1
  • 1
Tom
  • 77
  • 3

1 Answers1

1

This is a defined behavior of PHP when you compare a number value and a string value:

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

Use strict value comparison with === or !== and you’re getting the expected result.

Gumbo
  • 643,351
  • 109
  • 780
  • 844