PHP Tip: Comparing integers and numeric strings

This post describes how to compare variables containing integers using PHP.

To compare values more reliably in PHP, it can help to look at the intval() function. Without it, you may run into cases where two variables do not compare the way you expect. For example, your script may report that they are not the same.

$a = 5;
$b = '5';

if (intval($a) == intval($b)) {
    echo "it's the same";
}

Resources