2

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

'NONE' == 0

2 Answers 2

13

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

Sign up to request clarification or add additional context in comments.

2 Comments

This is just nitpicking, but is_numeric('16 foo') is FALSE, e.g. a non-numerical string and typecasting it to int will turn it into 16, not 0.
not in this case :) it's all described at de.php.net/manual/en/…
4

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.

5 Comments

As described here: php.net/manual/en/…
so ... NONE is a keyword? Like 'false'? (I'm asking this while reading the chapter)
@Putr no, its a string. nothing more.
@Putr Not really. Any string in a boolean context evaluates to true. So ('false' == false) is false, ('false' == true) is true.
@NullUser it's not a boolean context here though, but a numeric one, so ('10' == 10) and ('0xFF' == 255) and ('16 foo' == 16) is all true.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.