Home  >  Article  >  Backend Development  >  PHP Operator (6) "Comparison Operator" Example Explanation

PHP Operator (6) "Comparison Operator" Example Explanation

怪我咯
怪我咯Original
2017-04-12 15:18:472956browse

Through the previous series of PHP operator learning, today we learn the sixth type of PHP operator "Comparison operator".

What are comparison operators used for?

Comparison operator. You understand it literally and it is used for comparison. It compares the results of two variables or expressions to determine whether they are true or false. If the result of the comparison is true, , returns true, otherwise, if the comparison result is false, returns false.

Let’s first take a look at the comparison operators in PHP

Comparison operators

##> is greater than $x>$yIf $x is greater than $y, return trueLess than or equal to $xIf $x is less than or equal to $y, return true. >=Greater than or equal to $x>=$yIf $x is greater than or equal to $y, then returns true. ==Equal$x==$yReturns true if $x is equal to $y. !=Not equal$x!=$yIf $x is not equal to $y, return true===Identical (congruent)$x===$yif $x Equal to $y, and they are of the same type, return true!==non-identity (not congruent)$x!= =$yReturns true if $x is not equal to $y and they are not of the same type


PHP Operator (6) Comparison Operator Example ExplanationThere are two comparison operators that need attention, namely "===" and "!==". If you use the "===" operator for comparison, they must not only be equal in value, but also have the same data type. For example, $a===$b means that $a and $b are not only completely equal in value, And the data types of $a and $b are also the same. ! == and === have opposite meanings, such as $a! ==$b means that $a and $b either have different values ​​or different data types.

Comparison operator example

This example uses comparison operators to compare the values ​​of variables. Set the variable $x=100, the data type is integer, and the variable $ y="100", the data type is string, compare $x and $y, use "==", "===", "!=", "!===" operators.

The code is as follows

<?php
$x=100;
$y="100";

var_dump($x == $y);
echo "<br>";
var_dump($x === $y);
echo "<br>";
var_dump($x != $y);
echo "<br>";
var_dump($x !== $y);
echo "<br>";

?>

Code running results:

PHP Operator (6) Comparison Operator Example Explanation

The other ones are relatively simple, so I won’t do too many demonstrations here. , if you are interested, you can do it yourself. In the next section, we will explain to you the seventh type of PHP operator "Error Control Operator".

Recommended related articles:

1.PHP operators (1) "Arithmetic operators" examples explained

2.PHP operations Operator (2) "String Operator" Detailed Example Example

3.PHP Operator (3) "Assignment Operator" Example Example

4. PHP Operator (4) "Bit Operator" Example Example

5.PHP Operator (5) "Logical Operator" Example Example


Operator Name Example Description
Less than $x Returns true if $x is less than $y.

The above is the detailed content of PHP Operator (6) "Comparison Operator" Example Explanation. 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