Lesson 6 – PHP Comparison and Logical Operators

Operators in PHP help you perform different types of tasks and functions on data. We have already used arithmetic operators in PHP Mathematics lesson. In the following lines, we take a look at the main types of operators in PHP.

Arithmetic Operators

These operators enable you to perform mathematical operations in PHP. Following is a complete list of arithmetic operators.

Let suppose we have two variables, $num1 and $num2 with values of 5 and 10 respectively. 5 and 10 in this example are known as operands.

Operator Description Example
+ Adds two operands $num1 + $num2 will give 15
Subtracts second operand from the first one $num1 – $num2 will give -5
* Multiply the two operands $num1 * $num2 will give 50
/ Divide numerator by de-numerator $num2 / $num1 will give 2
% Modulus Operator and remainder of after an integer division $num2 % $num1 will give 0
++ Increment operator, increases integer value by one $num1 ++ will give 6
Decrement operator decreases integer value by one $num1 — will give 4

Comparison Operators

Comparison operators help you compare two values or statements in PHP. There are quite a few comparison operators in PHP.

Let suppose we have two variables, $num1 and $num2, with values of 5 and 10 respectively.

Operator Description Example
== Determines if the values of two operands are equal or not.  If yes, then the condition becomes true. ($num1 == $num2) is not true.
!= Verifies if the values of two operands are equal or not. The condition becomes true if values are not equal. ($num1 != $num2) is true.
Verifies if the value of the left operand is greater than the value of right operand. If yes, then condition becomes true. ($num1 > $num2) is not true.
Verifies if the value of the left operand is less than the value of right operand. If yes, then condition becomes true. ($num1 < $num2) is true.
>= Verifies if the value of the left operand is greater than or equal to the value of right operand. If yes then condition becomes true. ($num1 >= $num2) is not true.
<= Checks if the value of the left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

Assignment Operators

As the name suggests, we use assignment operators to assign values to variables and arrays. Here is a complete list of assignment operators in PHP.

Let suppose we have two variables, $num1 and $num2, with values of 5 and 10 respectively.

Operator Description Example
= Assigns values from right side operands to left side operand $num3 = $num1 + $num2 will assign value of $num1 + $num2 into C
+= Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand $num3 += $num1 is equivalent to $num3 = $num3 + $num1
-= Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand $num3 -= $num1 is equivalent to $num3 = $num3 – $num1
*= Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand $num3 *= $num1 is equivalent to $num3 = $num3 * $num1
/= Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand $num3 /= $num1 is equivalent to $num3 = $num3 / $num1
%= Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand $num3 %= $num1 is equivalent to $num3 = $num3 % $num1

Logical Operators

Logical operators check whether or not a condition is true.

Let suppose we have two variables, $num1 and $num1, with values of 5 and 10 respectively.

Operator Description Example
and Called Logical AND operator. The condition becomes true if both the operands are true. ($num1 and $num1) is true.
or Called Logical OR Operator. The condition becomes true if any of the two operands are non-zero ($num1 or $num1) is true.
&& Called Logical AND operator. The condition becomes true if both the operands are non-zero ($num1 && $num1) is true.
|| Called Logical OR Operator. The condition becomes true if any of the two operands are non-zero ($num1 || $num1) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !( $num1 && $num1) is false.

In the next lesson, we will study Decision Making or Conditional Statements in PHP.