Comparison operators, or relational operators, test some type of relation between two values. We've seen a few comparison operators in math, such as greater-than and less-than; in PHP, we have a few more comparison operators at our disposal.
The relational operators that we're going to be looking at are:
Greater-than
Greater-than-or-equal-to
Less-than
Less-than-or-equal-to
Equal-to
Identical-to
Not-equal-to
Not-identical-to
Spaceship operator
The comparison operator, whichever one you choose, will evaluate to either true or false. If we say, 3 greater than 2, what we're really saying is:
Is 3 greater than 2?
If we can say YES than that evaluates to a true statement. If we say NO, than that evaluates to a false statement.
Let's start by assigning a few values to some variables. Remember, single equal sign means assignment; double equal sign means equality.
$a = 10;
$b = 5;
$c = "10";
We'll use the var_dump() function to help us figure out whether the expression will evaluate to true or false; it will just dump the result of the expression.
Let's start with the equality operator, ==. We want to see whether $a equals $b. Is 10 equal to 5? No, so it's false.
var_dump( $a == $b ); // false
Let's note the different data types for each of our variables.
$a stores an integer
$b stores an integer
$c stores a string
Let's try the equality operator again, this time between $a and $c. It comes out to true? Why? PHP looks at the string "10" and sees that it contains a number inside; it's a numeric string. It loosely compares the two values and sees that they do in fact match.
The equality operator looks at only the value, not the data type.
var_dump($a == $c); // true
If the data types are important, the identity operator, ===, is used. It compares both the value and the data type. If they don't match, the expression will return false.
var_dump($a === $c); // false
Similarly, if you want to see if the two values are not equal, you can use the "not-equal-to" operator (!=). If the two values are not equal, it returns true. Remember, what we're asking is: is 10 not equal to 5? That's correct, 10 does not equal 5, so that is a true statement.
var_dump($a != $b); // true
The not-equal-to operator tests only the values of the expression, not the data type. To test the data types along with the values, we would use the not-identical-to operator (!==).
var_dump($a != $c); // false
var_dump($a !== $c); // true
Greater-than, less-than, greater-than-or-equal-to, and less-than-or-equal-to are straight forward. I'm sure that you've seen these operators in action at some point in your life.
There is no greater-than-and-identical-to operator or less-than-and-identical-to-operator.
The last operator is called the spaceship operator. It returns 0 when the values are equal, 1 when the right side is greater than the left side, and -1 when the left side is greater than the right side.
0 when equal
1 when right is larger
-1 when left is larger
Although we looked at integer values, we can compare any data type. For example, we can compare $a to the boolean value true. We get a weird result: true. Why? Any integer, except zero, when compared to true, is equal to true. This can cause some issues if you accidentally pass a number into a conditional statement, like the if statement. We'll look at that later.
Read the full article on my website
https://www.dinocajic.com/php-compari...
Code for this tutorial
https://github.com/dinocajic/php-7-yo...
Full Code
https://github.com/dinocajic/php-7-yo...
PHP Playlist
• PHP Tutorial
--
Dino Cajic
Author and Head of IT
Homepage: https://www.dinocajic.com
GitHub: https://github.com/dinocajic
Medium: / dinocajic
Instagram: / think.dino
LinkedIn: / dinocajic
Twitter: / dino_cajic
My Books
An Illustrative Introduction to Algorithms
https://www.amazon.com/dp/1686863268
Laravel Excel: Using Laravel to Import Data
https://amzn.to/4925ylw
Code Along With Me - PHP: From Basic to Advanced PHP Techniques
https://amzn.to/3M6tlGN
Auf dieser Seite können Sie das Online-Video Comparison Operators - PHP - P17 mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer Dino Cajic 03 März 2020 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 212 Mal angesehen und es wurde von 3 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!