Operators in PHP
Operators are used to perform some operation.
For example : addition of two numbers is an operation.
PHP operators are divided into the following groups :
1.Arithematic Operators
2.Assignment Operators
3.Comparison Operators
4.Logical Operators
5.Increment and Decrement Operators
1.Arithematic Operators
These operators are used to perform arithematic operations such as addition, subtraction of two numbers,etc.
1A) "+" Operator
It is used to perform addition of two or more variables/values.
Syntax : Variable1 + Variable2
Example :
<?php
$a = 2;
$b = 8;
$result = $a + $b;
echo $result; //10 will be the output
?>
1B) "-" Operator
It is used to perform subtraction of two or more variables/values.
Syntax : Variable1 - Variable2
<?php
$a = 10;
$b = 5;
$result = $a - $b;
echo $result; //5 will be the output
?>
1C) "*" Operator
It is used to perform Multiplication of two or more variable/value.
Syntax : Variable1 * Variable2
<?php
$a = 10;
$b = 5;
$result = $a * $b;
echo $result; //50 will be the output
?>
1D) "/" Operator
It is used to perform division of two or more variables/values.
Syntax : Variable1 / Variable2
<?php
$a = 10;
$b = 5;
$result = $a / $b;
echo $result; //2 will be the output
?>
1E) "%" Opertor
It gives the remainder of two or more variables/values.
Syntax : Variable1 % Variable2
<?php
$a = 10;
$b = 5;
$result = $a % $b;
echo $result; //0 will be the output
?>
2.Assignment Operators
These operators are used to assign values to a variable.
Let's consider our variables are $a and $b.
2A) "=" Operator
$a = $b. Here $a is assigned the value of $b;
2B) $a += $b
This is equal to writting $a = $a + $b;
2C) $a -= $b
This is equal to writting $a = $a - $b;
2D) $a *= $b
This is equal to writting $a = $a * $b;
2E) $a /= $b
This is equal to writting $a = $a / $b;
2F) $a %= $b
This is equal to writting $a = $a % $b;
3.Comparison Operator
These operators are used to compare two variables/values.
Let's consider our variables are $a and $b.
3A) $a == $b
Returns true when $a is equal to $b.
3B) $a != $b
Returns true when $a is not equal to $b.
3C) $a > $b
Returns true when $a is greater than $b.
3D) $a < $b
Returns true when $a is lesser than $b.
3E) $a >= $b
Returns true when $a is greater than or equal to $b.
3F) $a <= $b
Returns true when $a lesser than or equal to $b.
4.Logical Operators
These operators are used to combine two or more conditional statements.
Let's consider our variables are $a and $b.
4A) $a && $b
Returns true when both $a and $b are true.
4B) $a || $b
Returns true when either $a or $b is true.
4C) !$a
Returns true when $a is not true.
5.Increment and Decrement Operators
These operators are used to increase or decrease the value of a variable.
Let's consider our variables are $a and $b.
5A) $a++
This is known as post-increment operator. It returns the value of $a and then increments the value of $a by 1.
5B) $a--
This is known as post-decrement operator. It returns the value of $a and then decrements the value of $a by 1.
5C) ++$a
This is known as pre-increment operator. It increments the value of $a by 1 and then returns the value of $a .
5D) --$a
This is known as pre-decrement operator. It decrements the value of $a by 1 and then returns the value of $a .