PHP type comparison


  • Loose comparison: Use two equal signs == for comparison, only comparing values, not types.

  • Strict comparison: use three equal signs === to compare, in addition to comparing values, also compare types.

For example, "42" is a string and 42 is an integer. FALSE is a boolean value and "false" is a string.

<?php
if (42 == "42") {
    echo '1、值相等';
}
echo PHP_EOL; // 换行符
if (42 === "42") {
    echo '2、类型相等';
} else {
    echo '3、不相等';
}
?>

The output result of the above example is:

1, the values ​​are equal

3, don’t want to wait

Compare 0, false, null in PHP

Test code:

Example:

<?php
echo '0 == false: ';
var_dump(0 == false);
echo '0 === false: ';
var_dump(0 === false);
echo PHP_EOL;
echo '0 == null: ';
var_dump(0 == null);
echo '0 === null: ';
var_dump(0 === null);
echo PHP_EOL;
echo 'false == null: ';
var_dump(false == null);
echo 'false === null: ';
var_dump(false === null);
echo PHP_EOL;
echo '"0" == false: ';
var_dump("0" == false);
echo '"0" === false: ';
var_dump("0" === false);
echo PHP_EOL;
echo '"0" == null: ';
var_dump("0" == null);
echo '"0" === null: ';
var_dump("0" === null);
echo PHP_EOL;
echo '"" == false: ';
var_dump("" == false);
echo '"" === false: ';
var_dump("" === false);
echo PHP_EOL;
echo '"" == null: ';
var_dump("" == null);
echo '"" === null: ';
var_dump("" === null);

The output result of the above example is:

##0 == false: bool(true)0 === false: bool(false)

0 == null: bool(true)
0 === null: bool(false)

false == null: bool( true)
false === null: bool(false)

"0" == false: bool(true)
"0" === false: bool(false)

"0" == null: bool(false)
"0" === null: bool(false)

"" == false: bool(true)
"" === false: bool(false)

"" == null: bool(true)
"" === null: bool(false)

1791863413-572055b100304_articlex.png

1.png