Jorge Zapata
Jorge Zapata

Reputation: 2336

Array key comparison fails

For some reason when I iterate through an array using foreach loop a condition fails comparing the key with a string. My array has two indexes the first one is an integer and the second one is an string.

$firmas[] = $credito['acreditado'];
$firmas['cbi'] = "LIC. MARCELA SOTO ALARCÓN";

I want to do something else when the loop finds that the key in that moment is the string one but for some reason when I evaluate the integer index the result is true.

 foreach($firmas as $key => $firma){
            var_dump($key);
            var_dump($key=='cbi');die();
        }

The output is

int(0) bool(true) 

But as you can see the condition is looking for the string 'cbi' so the result should be false with the integer index and true for the string.

What's happening here?

Upvotes: 1

Views: 64

Answers (1)

Explosion Pills
Explosion Pills

Reputation: 191749

In PHP, all strings are equal to 0, though not equivalent to it. Try using === instead of just ==.

Addendum: all strings that do not begin with numbers are equal to 0.

Upvotes: 2

Related Questions