Home >Backend Development >PHP Tutorial >How to use php comparison operators to compare different types

How to use php comparison operators to compare different types

伊谢尔伦
伊谢尔伦Original
2017-06-21 14:22:091671browse

1. PHP comparison operators

$a == $b equals TRUE if $a equals $b.
$a === $b Congruent TRUE if $a is equal to $b and their types are also the same. (Introduced in PHP 4)
$a != $b is not equal to TRUE if $a is not equal to $b.
$a a8093152e673feb7aba1828c43532094 $b is not equal to TRUE if $a is not equal to $b.
$a !== $b is not congruent TRUE if $a is not equal to $b, or their types are different. (PHP 4 only)
$a 17c560c70c4bee8e0c4ffcbdcae8bd10 $b is greater than TRUE if $a is strictly $b.
$a 83a8a2f00d3b9efd5591758334f575d3= $b is greater than or equal to TRUE if $a is greater than or equal to $b.

If a PHP comparison operator compares an integer to a string, the string will be converted to an integer. If comparing two numeric strings, compare as integers. This rule also applies to switch statements.

<?php  
var_dump(0 == "a"); // 0 == 0 -> true  
var_dump("1" == "01"); // 1 == 1 -> true  
switch ("a") {  
case 0:  
echo "0";  
break;  
case "a": // never reached because "a" is already matched with 0  
echo "a";  
break;  
}  
?>

If the types of the operands are different, compare according to the following table (in order).

2. PHP comparison operators compare different types

null or string string Convert NULL to "" for numerical or lexical comparison
bool or null Any other type is converted to bool, FALSE 9cca799bf9b8bd3341fcceb45f5c32ee, <, <=, =, !=)

d. Require the compared object to be unloaded in front.

The above is the detailed content of How to use php comparison operators to compare different types. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn