-1

I've got this PHP code:

$array = array( 0 => 'brick', 1 => 'brick', 2 => 'window' );
$array_2 = array( 0 => 'brick', 1 => 'brick', 2 => 'door' );

foreach ( $array as $key => $value) 
{
    $my_variable = array();

    foreach ( $array_2 as $key_2 => $value_2 ) 
    {
        $my_variable[ $value_2 ] = !isset( $my_variable[ $value_2 ] ) ?
                                    $key_2 :
                                    'several';

        echo var_dump( $my_variable[ $value_2 ] ) . '<br/>';
        echo var_dump( $my_variable[ $value_2 ] == 'several' ) . '<br/><br/>';                                                                                                      
    }
}

It outputs:

int(0) 
bool(true) 

string(7) "several" 
bool(true) 

int(2) 
bool(false) 

int(0) 
bool(true) 

string(7) "several" 
bool(true) 

int(2) 
bool(false) 

int(0) 
bool(true) 

string(7) "several" 
bool(true) 

int(2) 
bool(false)

The strange part of it is this one (the first output shouln't be true as it's not equal to 'several', as it says it's value is indeed equal to 0):

int(0) 
bool(true) 

string(7) "several" 
bool(true) 

Because of this condition:

$my_variable[ $value_2 ] == 'several'

So, the question is: does anyone know why is this happening? Is there something wrong in my code I'm ignoring...?

On the other hand: if I change the array ordering (make it to start from 1, not from 0), it magicaly starts to work (outpust the correct logic):

$array = array( 1 => 'brick', 2 => 'brick', 3 => 'window' );
$array_2 = array( 1 => 'brick', 2 => 'brick', 4 => 'door' );

It's also possible to switch the condition (without a need to change array numbering) to:

!is_string( $my_variable[ $value_2 ] )

It than maybe outputs the correct logic, but the the mystery (why PHP outputs true if it should output false) still remains. And I'm afraid it could bring some troubles in the future when the code will be in the production. I'm also not happy from not knowing whether I'm possibly doing something wrong here...?

Based on the comments I would like to make some statements, just to make clear why I'm doing some things in the code above the way I do:

  • I'm looping inside other loop in order to chache the values from the array_2, (just in my real code, not here, code here is just substraction of it, to make question simpler, sorry for the confusion).

  • I'm echoing var_dump() in order to be able to add the <br/> string (thanks for <br/>, I totaly overlooked it.) - corrected

  • Yes, $my_variable = null; should be $my_variable = array();, but again only here (in the real code I assign array() later and only if needed). - corrected

Michal
  • 43
  • 8
  • Why are you looping inside your other loop, what do the 2 arrays have to do with each other? Why are you doing `echo var_dump();`? (`var_dump()` will echo for you.) Shouldn't `$my_variable = null;` be `$my_variable = array();`? P.S. `` should be `
    `.
    – gen_Eric Sep 15 '16 at 16:06
  • This might solve your confusion use tripple equal instead or double in var_dump() http://stackoverflow.com/questions/80646/how-do-the-php-equality-double-equals-and-identity-triple-equals-comp – Farhan Sep 15 '16 at 16:13

1 Answers1

2

The issue here is that you are using ==.

This will compare value, and will also convert values so that they are the same type.

0 == 'several'

These two types are not the same. Because of this, PHP will convert 'several' into a number - 0. You will then get 0 == 0, which is true.

The reason it "worked" when you changed the indexes was that you were instead doing

`1 == 'several'`

Which PHP interpreted as 1 == 0 which is false.


What you want to use is ===. This will compare both type and value.

0 === 'several'

This will be false since one is a number and the other a string.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • This was the exact reason why it behaved this wierdly. Awsome, thanks a lot for help! I would also like to highlight the overall topic of this tripple equal meantioned in the second comment under the question by @farhan. Thanks. – Michal Sep 15 '16 at 17:33