0

I have following example where PHP IF is giving wrong results, I don't understand why?

Results are correct in these example

$i=0; // i is set to int 0
if( $i == 0){echo 'yes';}else{echo 'no';}echo '<br>';
result yes

$i='string'; // i is set as a string
if( $i == 'string'){echo 'yes';}else{echo 'no';}echo '<br>';
result yes

But results are wrong in these examples

$i='string'; // i is set as a string
if( $i == 0){echo 'yes';}else{echo 'no';}echo '<br>';
result yes

$i=0; // i is set to int 0
if( $i == 'string'){echo 'yes';}else{echo 'no';}echo '<br>';
result yes

Please suggest why this error in comparing, why PHP IF is not comparing correctly.

Thanks

  • 1
    Im sorry but the question this is marked to as a duplicate doesn't really answer OP's question. Anyway I can give you this: http://il.php.net/manual/en/types.comparisons.php . Look at the **Loose comparisons with ==** – Loko Jun 16 '17 at 13:32
  • Thanks for the link. Now I understand better. –  Jun 18 '17 at 02:22

0 Answers0