2

I'll post a question and I hope the administrators stackoverflow do not put this as a duplicate topic , because I find no explanation for such a thing .

All we know that after the type- juggling this comparison looks like this:

false == array() -> (bool) == false (bool) array() -> false == false -> true

So far, nothing unusual.

The problem was trying to ask for an explanation for such a thing but the answers did not have senses and then it seemed that the topic was duplicated , but actually it is not a duplicate topic.

The question was how the php interprets this comparison below:

'' == array()

Some people said:

"A cast is done to the empty string to array"

The result is:   (array) '' == array() -> array('') == array() -> false

Seems to be right.

But the big problem is here, below:

'' == array('')

Because it does not return true ? If by some theory:

'' == array('') -> This should return true

'' == array('') -> (array)'' == array('') -> array('') == array('')

But false is returned.

Can you explain once and for all this issue ?

6
  • 6
    When an array is compared to a string, the array is greater. (php.net/manual/en/…) Commented Jan 3, 2014 at 7:38
  • In short: garbage in - garbage out You should decide first, why you need to compare arrays and strings. I'm 99% sure, that in normal situation you will always compare entities of same type Commented Jan 3, 2014 at 7:40
  • Jonny, can u explain ? Commented Jan 3, 2014 at 8:07
  • Oops, that was the wrong language link, here again the right one: PHP type comparison tables Commented Jan 3, 2014 at 8:10
  • @user3151657 My logic says, a bag can contain apples var_dump(array() > "");, but an apple no bags var_dump("" > array()); And an array("") even contains a string var_dump(array("") > ""); like a bag with an apple :D Commented Jan 3, 2014 at 8:21

1 Answer 1

3

tl;dr no casting is performed in the comparison:

Internally the comparison is done via compare_function, which is called when the parser sees a == operator.

Inside this function a test is done based on the type of each operand; the combination of string and array doesn't have a specific behaviour, so a numeric conversion is attempted here.

This attempt fails for both operands, because an empty string is not numeric and an array isn't either. It then performs another test here to check whether any operand is an array or an object. If so, it either returns -1 or 1, depending on which operand matched.

Of course, this is also documented in the manual to help those who don't want to decipher the source code :)

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

1 Comment

I thought there was rules for all possible types of comparison. What I did understand was that it was always made ​​made ​​one cast to the type of variable that had more weight. But I liked that explanation.

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.