Why a string value is 0


Can someone please explain why does the following return true??

var_dump('foo' == 0);



$str = 'foo';
if ($str == 0) {
} else {
  echo '$str does not equal 0';


Because if you cast ‘foo’ to an integer you get 0

var_dump((int)'foo'); // int(0)

And when you use == PHP will cast one of the operands (in this case the string, PHP prefers to compare numbers over anything else) to the type of the other operand. So they are both 0 and PHP deems them to be equal.

You can overcome this by using === instead of == which tells PHP that the values and types of the operands must be the same

var_dump(0 === 'foo'); // bool(false)

This is the exact reason why I stay clear of == most of the time and use === instead (in combination with casting where needed).

Agreed. To me, it’s kind of like making class properties private by default – you should only increase visibility when it is required. There aren’t too many instances where == is preferred over ===; I’ve found that more often than not, when I use ==, it’s because I am being lazy.

Excellent explanation.


BTW, I have one question.

When using === does not PHP do type casting? does it means when doing only == php casts the type but not when doing === ??

Pls confirm

Correct, because with === PHP first checks if the operands are of the same type (both strings, or both ints, etc), and if they’re not it evaluates to false.
Only when the types match will it compare the operands. So casting is not applicable at all with ===.

Basically that’s the difference between how == and === work.

== casts both to the same type before comparing
=== returns false if they are not the same type and only compares them if they are

There are very few situations where == is useful and lots of situations where it can cause errors. It is best avoided by all but the most experienced PHP programmer who knows when those rare instances occur where it is useful.

This should be enlightening… http://phpsadness.com/sad/52

How does it know which one to cast?

I mean when comparing a string with an integer, which one of it should it cast and what to?

also what about when comparing a boolean value with an array? Similarly, an object with a null value?

Thanks in advance

There is an order of precedence in PHP that gets used to work out which to cast to what eg. strings get cast to numbers.

That it isn’t obvious what the order is and the order is not the same as in other programming languages (eg. in JavaScript numbers get cast to strings) is one of the reasons for NOT using ==

Here we explicitly cast the number to a string before comparing them so that we know which one will be cast directly from the code.

$num = 1;
$str = '1';
if ( $str === (string)$num ) echo 'True';

Here is a very extensive blog post about how == works internally. I suggest you get a drink first, because it’s a lot of information :slight_smile:

If === is called strict comparison, what == is called? Soft comparision?

I would say loose comparison, though I’m not 100% sure that’s the official term.
Why the interest? Are you writing a paper on it or something?

I don’t give much thought as to what they’re called, I’ve seen them so often I just know what they are.

But curious, I looked at the docs

Comparison Operators
Example … Name … Result
$a == $b … Equal … TRUE if $a is equal to $b after type juggling.
$a === $b … Identical … TRUE if $a is equal to $b, and they are of the same type.

Most of the better web sites I have seen refer to == as “an error waiting to happen” and simply refer to === as a comparison.