comparing two variables returns false result

后端 未结 5 1070
感情败类
感情败类 2021-01-25 13:27

Why does this always return true:

$s = \'334rr\';
$i = (int)$s;

if ($i == $s) {
    echo true;
} else {
    echo false;
}

If I ec

5条回答
  •  生来不讨喜
    2021-01-25 13:51

    From the manual:

    If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

    So: ($i == $s) is the same as ($i == (int)$s) for the values you've given.

    Use === to avoid type-juggling.

提交回复
热议问题