Operators in PHP
Before discussing operators in PHP we should know what is operator & operand? The symbols which are used to perform mathematical or logical operations are known as operator.
For Ex. +, - , *, /, &&, || etc.
While operand means the variables or values on which the operators will be operated upon.
For Ex. A + B. Here + is operator, A & B are operands.
Let’s start discussing operators in PHP now. An operator takes some values(we call them operands) and performs specific operation.
In PHP there are different types of operators available which are listed below.
- Arithmetic Operators
- Relational Operators
- Logical Operators
- Assignment Operators
- Conditional Operators(Turnery Operator)
Arithmetic Operators
Following are the available list of arithmetic operators with their meaning. Consider value of $A=10 & $B=5.
Operator |
Description |
Example |
Output |
+ |
Adds two operands |
$A + $B |
15 |
- |
Subtracts one operand from another |
$A – $B |
5 |
* |
Multiplies two operands |
$A * $B |
50 |
/ |
Divides first operand with the second operand |
$A / $B |
2 |
% |
Gives the reminder after division operation |
$A % $B |
0 |
++ |
Increments the value of operand by one |
$A++ |
11 |
-- |
Decrements the value of operand by one |
$B-- |
4 |
Relational Operators
Following are the available list of relational operators with their meaning. Consider value of $A=10 & $B=5.
Operator |
Description |
Example |
Output |
> |
Returns true if operand 1 is greater than operand2 |
$A > $B |
True |
< |
Returns true if operand 1 is less than operand2 |
$B < $A |
True |
>= |
Returns true if operand 1 is greater than or equal to operand2 |
$A >= $B |
True |
<= |
Returns true if operand 1 is less than or equal to operand2 |
$B <= $A |
True |
== |
Equality operator, returns true if operand1 and operand2 are equal ( checks only value and not data type) |
$A == $B |
False |
!= |
Inequality operator, returns true if operand1 and operand2 are not equal ( checks only value and not data type) |
$A != $B |
True |
Difference between == & === operator
“==“ compares two values/variables if they are same regardless of their data types it returns true. But when “===“ operator is used it returns true only when both the values/variables have same value and they are of same data type.
<?php $a='1'; $b=1; if($a==$b)//use === instead of == for the other result { echo "true"; } else { echo "false"; } ?>
As shown in above example the result of $a == $b will be true even though $a is string where as $b is integer as long as they both are storing same value , but for $a == $b the result will be false as the type of $a and $b is not same.
Logical Operators
Following are the available list of logical operators with their meaning.
Operator |
Description |
Example |
AND / && |
Returns true if all the conditions are true |
$A > 0 && $A < 10 $A > 0 AND $A < 10 |
OR / || |
Returns true if one of the condition is true |
$A > 0 || $A < 10 $A > 0 OR $A < 10 |
NOT / ! |
Reverses the expression. That means if the result of condition is true it returns false and if the result is true than it returns false |
!($A > 0) NOT($A > 0) |
Assignment Operators
Following are the available list of logical operators with their meaning. Consider value of $A=10 & $B=5.
Operator |
Description |
Example |
Output |
= |
Used for assigning the values |
$C = $A |
$C will get value of $A |
+= |
Adds right operand to left operand and assigns the result to left operand |
$A += $B |
$A = $A + $B |
-= |
Subtracts right operand from left operand and assigns the result to left operand |
$A -= $B |
$A = $A - $B |
*= |
Multiplies right operand with left operand and assigns the result to left operand |
$A *= $B |
$A = $A * $B |
/= |
Divides left operand with left operand and assigns the result to left operand |
$A /= $B |
$A = $A / $B |
Conditional Operator (Turnery Operator)
The conditional operator “ ?: ” first checks the condition for true or false and depending on the result the statements given will be executed. If condition is true statement written between ? and : is executed , if false statement followed by : will be executed. In sort conditional operator is used as alternative for simple if else statement.
For Ex.
<?php $a=10; $b=20; $c=($a>$b) ? $a : $b; echo $c; ?>
In above example conditional operator is used which checks value of $a and $b , if value of $a is greater than $b it will initialize $c with value of $a and if $a is less or equal to $b than it will initialize $c with value of $b.
So to conclude we have learnt different types of operators and their examples in this tutorial.
Share this Article on Social Media
Get Updates, Scripts & Other Tutorials to Directly to your Email
Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.
Comments