This chapter is about the PHP Operators.
What is Operator?:
As its name suggests “operator” is a thing which can operate on different data. Or we can say that it is used to perform certain operations on data. PHP Operators are used in almost every operation you perform while programming.
To elaborate operator definition let’s see this example of PHP Operators:
2 + 4 * 5
Here 2 + 4 * 5 is an expression, 2,4,5 are operands and ‘+’ and ‘*’ are operators, which are used to manipulate the expression to get output.
Here “+” and “*” are operating on numbers 2, 4 and 5 to perform certain operations and to generate the desired output.
There are different types of PHP Operators:
- Arithmetic
- Comparision
- Logical
- Assignment
- Conditional
Let’s see all the PHP Operators or operator types:
- Arithmetic operators
Operator | Example | Output | |
Addition | + | $a= 1; $a =4 + $a; | 5 |
Subtraction | – | $b = 15; $b = $b – 5; | 10 |
Multiplication | * | $c = 3; $c = $c * 5; | 15 |
Division | / | $d = 10; $d= $d/2 | 5 |
Modulus | % | $e = 15; $e = $e/2 | 1 |
Increment | ++ | $f = 10; $f++; | 11 |
Decrement | — | $g=20; $g–; | 19 |
2. Comparision Operators
Operator | Example | Description | |
Greater Than | > | 3 > 2 | This will check if the left value is greater than right value. |
Less Than | < | 5 < 10 | This will check if the left value if smaller than right value |
Greater than or equal to | >= | 12 >= 15 | This will check if the left value is greater or equal to left value. |
Less than or equal to | <= | 14 <= 12 | This will check if the left value is smaller or equal to left value. |
Equals | == | 15 == 15 | This will check if both left and right values are equal. |
Not equal | != | 14!= 15 | This will check if both left and right values are not equal. |
Triple Equals | === | 15 === ’15’
(Output-Not equal) |
This will check if both left and right values are equal or not with their data types also. If data types are not equal then values are not equal. |
3. Logical operators.
Operator | Example | Description | |
Logical AND | && | $a = 10; $b = 20; $a && $b; | If both the condition/values are ture/non zero then result will true. |
Logical OR | || | $a = 10; $b = 20; $a || $b; | If any of the condition/value is ture/non zero then result will true. |
Logical NOT | ! | $a = 10; $b = 20; !($a || $b); | This will reverse the result. If result is true then it will give false. |
Logical AND | and | $a = 10; $b = 20; $a AND $b; | If both the condition/values are ture/non zero then result will true. |
Logical OR | OR | $a = 10; $b = 20; $a OR $b; | If any of the condition/value is true/non-zero then result will true. |
4. Assignment Operator
Name | Operator | Example | Description |
Simple Assignment | = | A = B + C | Will assign result of B + C to A |
Add and assign | += | A += B | It will first add A to B and then assign result to A |
Subtract and assign | -= | A -= B | It will first add B from A and then assign result to A |
Multiply and assign | *= | A *= B | It will first multiply A and B and then assign result to A |
Divide and assign | /= | A /= B | It will first divide A by B and then assign result to A |
Modulus and assign | %= | A %= B | It will first find modulus of A and B and then assign result to A |
5. Conditional Operator
Operator | Example | Description | |
Conditional Expression | ?: | echo (A && B)?”Y” : “N” | It will print “Y” if condition (A && B) is true else will print “N” |
In this operator, if condition present before question mark “?” is true then statement before colon “:” will execute else statement after colon will execute.
Above PHP operators can be used in all of the PHP programs. You will see in details about their use in upcoming tutorials.