PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
Comparison operators in PHP are used to compare two values. They are a fundamental part of control structures like if
, while
, and for
.
Here's a tutorial on how to use comparison operators in PHP:
1. Equal (==
)
The ==
operator checks if the values of two operands are equal or not. If yes, then the condition becomes true.
if (5 == 5) { echo "True"; // Outputs: True }
2. Identical (===
)
The ===
operator checks if the values of two operands are equal and they are of the same type. If yes, then the condition becomes true.
if (5 === "5") { echo "True"; } else { echo "False"; // Outputs: False }
3. Not Equal (!=
or <>
)
The !=
or <>
operator checks if the values of two operands are not equal. If the values are not equal, then the condition becomes true.
if (5 != 10) { echo "True"; // Outputs: True }
4. Not Identical (!==
)
The !==
operator checks if the values of two operands are not equal or they are not of the same type.
if (5 !== "5") { echo "True"; // Outputs: True }
5. Greater Than (>
)
The >
operator checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition becomes true.
if (10 > 5) { echo "True"; // Outputs: True }
6. Less Than (<
)
The <
operator checks if the value of the left operand is less than the value of the right operand. If yes, then the condition becomes true.
if (5 < 10) { echo "True"; // Outputs: True }
7. Greater Than or Equal To (>=
)
The >=
operator 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.
if (10 >= 10) { echo "True"; // Outputs: True }
8. Less Than or Equal To (<=
)
The <=
operator 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.
if (5 <= 5) { echo "True"; // Outputs: True }
Understanding and using comparison operators is crucial for controlling the flow of your PHP programs. They allow your program to make decisions based on the comparison of different values.
How to Use Comparison Operators in PHP:
$a = 10; $b = 20; // Comparison operators $result = ($a > $b); // Outputs: false
PHP Equality Operators (==
and ===
) Explained:
$value1 = 10; $value2 = '10'; // Equality operators $result1 = ($value1 == $value2); // Outputs: true $result2 = ($value1 === $value2); // Outputs: false
Using Inequality Operators (!=
and !==
) in PHP:
$number1 = 15; $number2 = '15'; // Inequality operators $notEqual1 = ($number1 != $number2); // Outputs: false $notEqual2 = ($number1 !== $number2); // Outputs: true
PHP Greater Than (>
) and Less Than (<
) Operators:
$x = 5; $y = 8; // Greater than and less than operators $greaterThan = ($x > $y); // Outputs: false $lessThan = ($x < $y); // Outputs: true
Combining Conditions with Logical AND (&&
) in PHP:
$temperature = 25; $timeOfDay = 'morning'; // Logical AND operator $isGoodWeather = ($temperature > 20 && $timeOfDay == 'morning'); // Outputs: true
PHP Logical OR (||
) Operator Examples:
$isWeekend = true; $isHoliday = false; // Logical OR operator $isFreeDay = ($isWeekend || $isHoliday); // Outputs: true
PHP Ternary Operator for Concise Comparisons:
$score = 85; // Ternary operator $result = ($score >= 70) ? 'Pass' : 'Fail'; // Outputs: Pass
Comparing Strings in PHP Using strcmp()
and strcasecmp()
:
$string1 = 'apple'; $string2 = 'banana'; // Using strcmp() for case-sensitive comparison $comparison1 = strcmp($string1, $string2); // Outputs: -1 // Using strcasecmp() for case-insensitive comparison $comparison2 = strcasecmp($string1, $string2); // Outputs: -1
PHP Null Coalescing Operator (??
) for Comparisons:
$username = null; // Null coalescing operator $loggedInUser = $username ?? 'Guest'; // Outputs: Guest
PHP Spaceship Operator (<=>
) for Combined Comparison:
$valueA = 10; $valueB = 15; // Spaceship operator $comparisonResult = $valueA <=> $valueB; // Outputs: -1
Chaining Comparisons in PHP with &&
and ||
Operators:
$age = 25; $isStudent = true; // Chaining comparisons $isEligible = ($age >= 18 && $age <= 30 && $isStudent); // Outputs: true