Skip to main content

Comparing integer variables using PHP

This post describes how to compare variables that have integers using PHP.

To properly compare variables in PHP, we are taking a look at the intval function. If you try to compare variables without this function, you can run into troubles. For example, your script returning that they are NOT the same.

<?php
$a=5;
$b='5';

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

Resources: