Comparison Operators
Introduction to Comparison Operators in PHP
Before we start discussing about comparison operators in PHP, let's understand what an operator is. In PHP, an operator is something that tells the compiler to perform specific mathematical or logical manipulations. PHP is rich in the number of operators and among them, one type is the comparison operators.
Comparison operators are used to compare two values (number or string) in PHP scripts.
Types of Comparison Operators
PHP has the following types of comparison operators:
==
(Equal): It checks if the value of two operands are equal or not. If yes, then the condition becomes true.===
(Identical): It checks if the value and the type of two operands are equal or not. If yes, then the condition becomes true.!=
or<>
(Not Equal): It checks if the value of two operands are equal or not. If the values are not equal, then the condition becomes true.!==
(Not Identical): It checks whether two operands are not equal and/or not of the same type. If the values are not equal or they are not of the same type, then the condition becomes true.>
(Greater than): It checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition becomes true.<
(Less than): It checks if the value of the left operand is less than the value of the right operand. If yes, then the condition becomes true.>=
(Greater than or equal to): It checks if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition becomes true.<=
(Less than or equal to): It checks if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition becomes true.<=>
(Spaceship): This operator returns -1, 0 or 1 when $a is respectively less than, equal to, or greater than $b.
Examples of Comparison Operators
Here are some examples of the various comparison operators:
<?php
$x = 10;
$y = 20;
var_dump($x == $y); // Returns: bool(false) because values are not equal
var_dump($x === $y); // Returns: bool(false) because types or values are not equal
var_dump($x != $y); // Returns: bool(true) because values are not equal
var_dump($x !== $y); // Returns: bool(true) because types and values are not equal
var_dump($x > $y); // Returns: bool(false) because 10 is not greater than 20
var_dump($x < $y); // Returns: bool(true) because 10 is less than 20
var_dump($x >= $y); // Returns: bool(false) because 10 is not greater or equal to 20
var_dump($x <= $y); // Returns: bool(true) because 10 is less or equal to 20
var_dump($x <=> $y); // Returns: int(-1) because 10 is less than 20
?>
These comparison operators play a vital role in controlling the flow of the program. It helps in making decisions based on conditions. They always return a boolean value, either true or false.
That's it for comparison operators in PHP. Practice these operators with different scenarios and values to get a firm grasp of how they work. Happy coding!