How to use php comparison operators to compare different types

伊谢尔伦
Release: 2023-03-10 22:40:01
Original
1587 people have browsed it

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 <> $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 < $b is less than TRUE if $a is strictly less than $b.
$a > $b is greater than TRUE if $a is strictly $b.
$a <= $b is less than or equal to TRUE if $a is less than or equal to $b.
$a >= $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;  
}  
?>
Copy after login

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 < TRUE
object object Built-in classes can define their own comparisons. Different classes cannot be compared. Properties of the same class and arrays are compared in the same way (in PHP 4). PHP 5 has its own description

string , resource or number string, resource or number Convert strings and resources to numbers, compared by ordinary mathematics
array array An array with fewer members is smaller if the key in operand 1 does not exist in operand 2 Then the arrays cannot be compared, otherwise they will be compared one by one.

array Any other type of array is always larger
object Any other type of object is always larger

<?php  
// 数组是用标准比较运算符这样比较的  
function standard_array_compare($op1, $op2)  
{  
if (count($op1) < count($op2)) {  
return -1; // $op1 < $op2  
} elseif (count($op1) > count($op2)) {  
return 1; // $op1 > $op2  
}  
foreach ($op1 as $key => $val) {  
if (!array_key_exists($key, $op2)) {  
return null; // uncomparable  
} elseif ($val < $op2[$key]) {  
return -1;  
} elseif ($val > $op2[$key]) {  
return 1;  
}  
}  
return 0; // $op1 == $op2  
}  
?>
Copy after login

PHP comparison operatorternary operationoperator

Another conditional operator is the "?:" (or ternary) operator.

<?php  
// Example usage for: Ternary Operator  
$action = (empty($_POST[&#39;action&#39;])) ? &#39;default&#39; : $_POST[&#39;action&#39;];  
// The above is identical to this if/else statement  
if (empty($_POST[&#39;action&#39;])) {  
$action = &#39;default&#39;;  
} else {  
$action = $_POST[&#39;action&#39;];  
}  
?>
Copy after login

Expression (expr1) ? (expr2) : (expr3) The value of expr2 when expr1 evaluates to TRUE, and the value of expr3 when expr1 evaluates to FALSE.

Note: Note that the ternary operator is a statement, so its evaluation is not a variable, but the result of the statement. This is important if you want to return a variable by reference. The statement return $var == 42 ? $a : $b; in a function that returns by reference will not work, and a future version of PHP will issue a warning about this.

Identify operators to implement logical comparison

a. Multiple relationships that are directly OR (user input)

time>12
time=9
Copy after login

b. Single union relationship (user input)

time>12&&time<25
Copy after login

c. Available comparison operators (>, <, <=, =, !=)

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!