Hi Guys,
Today we discuss operators in php. The operators are in PHP helps to perform different functionalities in php. It is mainly used to perform operations in values and variables. An operator is one type symbol, that is used to perform certain operations on its operands. Operator is always associate with operand.
Example
x+y
Here x and y are the operand and ‘+’ is the operator
In PHP language supports following type of operators.
- Arithmetic Operators
- Logical Operators
- Conditional Operators
- Comparison Operators
- Assignment Operators
1) Arithmetic Operator
In php the arithmetic operator helps to perform common arithmetic operation like addition, multiplication, division etc. In other words, it performs mathematical operations on its operands.
Operator | Purpose | Example |
---|---|---|
+ | Perform addition of two operands. | $x=10; $y=2; $z=$x+$y; $z=12 |
– | Perform substraction of two operands. | $x=10; $y=2; $z=$x-$y; $z=8 |
* | Perform multiplication of two operands. | $x=10; $y=2; $z=$x*$y; $z=20 |
/ | Perform divide left operand by the right operand | $x=10; $y=2; $z=$x/$y; $z=5 |
% | Divide left operand by the right operand and return remainder of division | $x=11; $y=2; $z=$x/$y; $z=5 |
++ | Increment Operator, increase the integer values by 1. | $x=10; $x++ will give 11 |
— | Decrement Operator, decrease the integer values by 1. | $x=10; $x– will give 9 |
2) Logical Operators
It helps to perform logical operations on its operands.
Operator | Purpose | Example |
---|---|---|
&& | If both the operands are non zero then condition becomes true. | $x=true;$y=false;$z=$x && $y; $z=false; |
|| | If any of the two operands are non zero then condition becomes true. | $x=true;$y=false;$z=$x || $y; $z=true; |
! | If a condition is true then Logical NOT operator will make false. | $x=true; !($z=$x); $z=false; |
3) Conditional or Ternary Operator
There is one more operator called conditional operator. This first evaluates an expression for a true or false value and then execute one of the two given statements depending upon the result of the evaluation.
Operator | Purpose | Example |
---|---|---|
? : | It is used for conditional expression. | If Condition is true ? Then value X : Otherwise value Y |
4) Comparison Operator
It helps you to perform comparision betweeen two operands.
Operator | Purpose | Example |
---|---|---|
== | Checks if the value of two operands are equal or not, if yes then condition becomes true. | (x == y) is not true. |
!= | Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. | (x != y) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (x > y) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (x < y) is true |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (x >= y) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (x <= y) is true |
5) Assignment Operator
It allows you to assign value of one variable to another variable.
Operator | Purpose | Example |
---|---|---|
= | Simple assignment operator, Assigns values from right side operands to left side operand | z = x + y will assign value of x + y into z |
+= | Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand | z += x is equivalent to z = z + x |
-= | Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand | z -= x is equivalent to z = z – x |
*= | Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand | z *= x is equivalent to z = z * x |
/= | In here, it divides left operand with the right operand and assign the result to left operand | z /= x is equivalent to z = z / x |
%= | Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand | z %= x is equivalent to z = z % x |
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.