PHP - No results expected with == comparison operator

advertisements

With this code:

if($foo['bar'] == $baz) {
    $results[] = $foo;
}

I never reach the assignment $results[] = $foo; (with a certain value)

Now if I do (with this same, certain value):

strcmp(trim($foo['bar']), trim($baz));

I get a result of 0, indicating the strings are in fact equal. So... why in the world can't I reach $results[] = $foo;?

Shouldn't $foo['bar'] == $baz be true if a strcmp of the two equals 0? What am I missing?


By the way, it's only one particular comparison that fails. Other comparisons using the same if statement will work fine.

example:

when $foo['bar'] == 'meat' and $baz == 'meat' I get $results['meat']

but when $foo['bar'] == 'meat sauce' and $baz == 'meat sauce' I get $results[]


The observed behavior does not make sense, so there has to be a hidden gotcha somewhere. Let's start with the fact that trim seems to make it work.

Did you use var_dump to verify that $foo['bar'] and $baz really have the same value?