String comparison in PHP doesn't make sense … [duplicate] - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, May 31, 2021

String comparison in PHP doesn't make sense … [duplicate]

I have this really basic PHP code I can't figure out. In the example below, why do I get 'MATCH' when $i = 0, and not for the remaining rows …? It seems to be possible to workaround by using === instead of ==, but I can't understand why.

<?php

$text = 'foo bar';

for ($i = 0; $i <= 5; $i++) {
    echo '<p>' . $i . ': ';

    if ($i == $text) {
        echo 'MATCH!';
    } else {
        echo 'NOPE';
    }
}

?>


source https://stackoverflow.com/questions/67731189/string-comparison-in-php-doesnt-make-sense

No comments:

Post a Comment