Comparison and Logical Operators in PHP

Operators in PHP are used for different tasks like comparing or incrementing/decreasing values. They are also used to test logical operations. There are many types of operators that you will be using in PHP. They can be grouped according to their usage. Two main types of operators are comparison and logical operators. These conditions usually come together with conditional statements like IF. They are discussed below with examples.

Comparison Operator

This type of operator allows you to compare two or more values in PHP. You can compare the values and get results in true or false. Based on the result, you can take a separate path of action.

List of comparison operators:

== (Equal to): Compares two values and if both of them are the same, the result is TRUE.
!= or <> (Not Equal to): Compares two types of values and if both of them are not the same, the result is TRUE.
< (Less Than), > (Greater Than), <= (Less Than or Equal To), >=(Greater Than or Equal To): Compares whether values or higher, lower or same in value.
=== (Identical): Used for not only comparing the value but also comparing their data type like strings or integers.
!=== (Not Identical): If the data type like strings or integers do not match, then the result is true.

Logical Operator

These logical operators are used to combine multiple expressions. They are case insensitive.

List of logical operators:

AND or &&: If both the conditions are true then the result is also TRUE.
OR or ||: If either one of two conditions match then the result is TRUE.
XOR: If only one of the condition is true then the result is TRUE. If both or no condition come up as true, then the result is false.
NOT or !: The not operator is TRUE when the condition is false.

You may also like

Comments are closed here.


Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 2 other subscribers