PHP operators work on the variable or value which can be one or more. We can say that it takes some values or variables, performs the operation on those values, and gives the result. The operator can be “+”, “-“, “%”, etc.
For example:
5 + 6 = 11
In the above example, 5 and 6 are the values, “+” is the operator and gives the result 11.
PHP operators are divide into the following categories:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Increment/Decrement operators
- Logical operators
- String operators
- Array operators
- Conditional assignment operators
Now let us learn in detail about each of these operators:
Arithmetic Operators:
Arithmetic operators use simple math operations such as addition, subtraction, multiplication, etc.
Following is a full list of arithmetic operators:
Addition(+):
It works on more than one value and variable. Let’s take an example to understand.
Example:
1 2 3 | <?php echo 10 + 5; //Addition of two(20,5) values ?> |
Output:
1 | 15 |
Subtraction(-):
It is used to find the difference between two values or variables.
Example:
1 2 3 | <?php echo 20 - 10; ?> |
Output:
1 | 10 |
Multiplication(*):
Multiplication operator is used to multiplying two values or variables.
Example:
1 2 3 | <?php echo 5 * 10; ?> |
Output:
1 | 50 |
Division(/):
It is used for dividing the values or variables.
Example:
1 2 3 | <?php echo 50 / 10; ?> |
Output:
1 | 5 |
Modulus(%):
Modulus operator gives the remainder if we divide the two values or variables.
Example:
1 2 3 | <?php echo 17 % 5; ?> |
Output:
1 | 2 |
Exponentiation(**):
It is used to raise the power.
Example:
1 2 3 | <?php echo 2**3; ?> |
Output:
1 | 8 |
Let’s take more examples:
1 2 3 4 5 6 7 8 9 10 | <?php $a = 20; $b = 8; echo($a + $b); // 0utput = 28 echo($a - $b); // 0utput = 12 echo($a * $b); // 0utput: 160 echo($a / $b); // 0utput: 2.5 echo($a % $b); // 0utput: 4 echo($a ** $b); // 0utput: 25600000000 ?> |
Assignment operators:
Assignment operators(=) use a variable to assign value. The Assignment Operator’s left side operand is a variable, and the assignment operator’s right side operand is a value.
Operator | Name | Syntax | Same As |
---|---|---|---|
= | Assign | $a = $b | $a = $b |
+= | Addition and assign | $a += $b | $a = $a + $b |
-= | Subtraction and assign | $a -= $b | $a = $a – $b |
*= | Multiply and assign | $a *= $b | $a = $a * $b |
/= | Divide and assign quotient | $a /= $b | $a = $a / $b |
%= | Divide and assign modulus | $a %= $b | $a = $a % $b |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php $a = 5; echo $a; // Outputs: 5 $b = 6; $b += 8; echo $b; // Outputs: 14 $c = 20; $c -= 10; echo $c; // Outputs: 10 $d = 3; $d *= 7; echo $d; // Outputs: 21 $e = 16; $e /= 4; echo $e; // Outputs: 4 $f = 20; $f %= 5; echo $f; // Outputs: 0 ?> |
Comparison operators:
Comparison operators are required to check for certain differences or similarities between the values and variables. It works like booleans, giving back either true or false value.
Comparison operators are: >, <, >=, <=, ===, and !==.
Less than(<):
If the value on the left is less than the value on the right, it returns true, otherwise, it returns false.
Example:
1 2 3 4 | <?php echo (5<10);//Output true echo (10<5);//Output false ?> |
Greater than(>):
If the value on the left is greater than the value on the right, it returns true, otherwise, it returns false.
Example:
1 2 3 4 | <?php echo (10>5);//Output true echo (5>10);//Output false ?> |
Less than or equal to (<=):
If the left-hand value is less than or equal to the right-hand value, it returns true, otherwise, it returns false.
1 2 3 | <?php echo (10<=15);//Output true ?> |
Greater than or equal to (>=):
If the left-hand value is greater than or equal to the right-hand value, it returns true, otherwise, it returns false.
1 2 3 | <?php echo (20>=15);//Output true ?> |
Equal to (==):
If the left-hand value is equal to the right-hand value, it returns true otherwise it returns false.
1 2 3 | <?php echo (10==10);//Output true ?> |
Not equal to (!=):
If the left-hand value is not equal to right-hand value, it returns true otherwise, it returns false.
1 2 3 | <?php echo (10!=15);//Output true ?> |
Identical(===):
If the left-hand value is equal and the same data type to the right-hand value, it returns true otherwise it returns false.
1 2 3 | <?php echo (10===15);//Output true ?> |
Not Identical(!==):
If the left-hand value is not equal to the right-hand value or data type is different then it returns true otherwise it returns false.
1 2 3 4 5 6 7 | <?php echo (10==="10"); /* Output false because the value of the left hand is an integer and the value of right is considering as string. */ ?> |
Increment/Decrement operators:
It is used for increment or decrements the value of variables. The following are the increment or decrement operators:
Pre-increment(++$a):
It increments the value of variable $a first and return $a.
Post-increment($a++):
It returns the value of variable first and then returns $a.
Pre-decrement(–$a):
It decrements the value of variable $a first and returns $a
Post-decrement($a–):
It decrements the value of variable $a first and return $a.
Logical operators:
Following are the Logical operator which are used to check the conditions:
AND(&&): It returns true if all the condition is true otherwise returns false.
OR(||): It returns true if any of them is true otherwise returns false.
NOT(!): It returns true if the variable is not true.
String operators:
The following are the string operator:
Concatenation(.):
It is used to combine two strings.
Example:
1 2 3 | <?php echo "Hello"." World!"; //Output: Hello World! ?> |
Concatenation and assignment:
It combines the string and assigns.
Example:
1 2 3 4 5 6 | <?php $str1 = "Hello"; $str2 = " World!"; $str1 .= $str2; echo $str1; ?> |
Conditional assignment operators:
The following are the Conditional assignment operators:
Ternary(?:)
Example:
1 2 3 | <?php echo 20 > 10 ? "It's true" : "It's false"; ?> |
Null coalescing(??): It is a new operator introduced in PHP 7. It returns the first operand if it is set otherwise retund the second operand.
Example:
1 2 3 4 | <?php $a = 10; echo $a ?? 'null';//Output: 10 ?> |
In the above example, the value of $a is 10 so the output will be shown 10.
1 2 3 | <?php echo $a ?? 'null'; //Output: null ?> |
In this example, the value of $a is not set so the output will be shown null.
If you wish to share more information or have a query about the topic PHP Operators, please write comments.