Putr
Putr

Reputation: 967

PHP type matching

In PHP, can someone explain to me why this resolves to true:

'NONE' == 0

Upvotes: 2

Views: 347

Answers (2)

Gordon
Gordon

Reputation: 316969

Because the string is 0 when evaluated in a number context. Quoting:

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.

So it depends on what the string contains.

Also, see the chapter on Type Juggling and Type Comparison in the PHP Manual.

Upvotes: 4

NullUserException
NullUserException

Reputation: 85458

Because any non-numerical string cast to int will turn into 0.

If you don't want that to happen, use ===, the identical operator.

Read:
http://php.net/manual/en/language.operators.comparison.php
http://php.net/manual/en/types.comparisons.php

Upvotes: 13

Related Questions