Operators are used to perform some actions on variables and values. There are 7 types of operators.
1.PHP Arithmetic Operators
This operator is used with numerical values so that one can get value of a particular value.
Arithmetic operators are +, – , * , / , % , **(power)
Syntax:
Addition- $a + $b
Subtraction- $a – $b
Multiplication- $a * $b
Division- $a / $b
Modulus- $a % $b
Exponentiation- $a ** $b
Example: Use of PHP addition(+), subtraction(-), multiplication (*), division (/), modulo (%) and power(**) operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <!DOCTYPE html> <html> <head> <title>Arithmetic Operators in PHP</title> </head> <body> <?php $a = 15; $b = 3; $sum = $a + $b; $sub = $a - $b; $mul = $a * $b; $div = $a / $b; $mod = $a % $b; $pow = $a ** $b; echo 'sum = ' . $sum . '<br/>'; echo 'sub = ' . $sub . '<br/>'; echo 'mul = ' . $mul . '<br/>'; echo 'div = ' . $div . '<br/>'; echo 'mod = ' . $mod . '<br/>'; echo 'pow = ' . $pow . '<br/>'; ?> </body> </html> |
2. PHP Assignment Operators
It is used with numerical values when some need to write the value of a particular variable.
Assignment operators are =, +=, -=, *=, /=, %=, **=
Syntax:
a = b a = b Left operand shifts to right operand
a += b a = a + b Addition
a -= b a = a – b Subtraction
a *= b a = a * b Multiplication
a /= b a = a / b Division
a %= b a = a % b Modulus
Example: Use of PHP Assignment Operators (=, +=, -=, *=, /=, %=, **=)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <!DOCTYPE html> <html> <head> <title>Assignment Operators in PHP</title> </head> <body> <?php $a = 2; $b = 3; $c = 4; $d = 5; $e = 2; $f = 4; $g = 5; $h = 4; $c += $a; //or $c=$c + $a; $d -= $a; //or $d=$d - $a; $e *= $a; //or $e=$e * $a; $f /= $a; //or $f=$f + $a; $g %= $a; //or $g=$g % $a; $h **= $a; //or $h=$h ** $a; $i = $a; echo 'c = ' . $c . '<br/>'; echo 'd = ' . $d . '<br/>'; echo 'e = ' . $e . '<br/>'; echo 'f = ' . $f . '<br/>'; echo 'g = ' . $g . '<br/>'; echo 'h = ' . $h . '<br/>'; echo 'i = ' . $i . '<br/>'; ?> </body> </html> |
3. PHP Comparison Operators
== Equal
=== Identical
!= Not equal
<> Not equal
!== Not identical
> Greater than
< Less than >= Greater than or equal to
<= Less than or equal to
Example: Use of PHP equal (==) and (===) Identical operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <!DOCTYPE html> <html> <head> <title>Comparison Operators in PHP</title> </head> <body> <?php $a = 2; $b = 2; $c = "2"; if ($a == $b) { echo "Both are equal<br/>"; } if ($a == $c) { echo "Both are equal<br/>"; } if ($a === $c) { echo "Both are identical<br/>"; } else{ echo "Both are not identical"; } ?> </body> </html> |
1 2 3 | Both are equal Both are equal Both are not identical |
Example: Use of PHP (!=) Not equal and (<>) Not equal and (!==) Not identical operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <!DOCTYPE html> <html> <head> <title>Comparison Operators in PHP</title> </head> <body> <?php $a = 2; $b = 3; $c = "3"; $d = "2"; if ($a != $b) { echo "Both are not equal<br/>"; } if ($a <> $c) { echo "Both are not equal<br/>"; } if ($a !== $d) { echo "Both are not identical<br/>"; } ?> </body> </html> |
1 2 3 | Both are not equal Both are not equal Both are not identical |
Example: Use of PHP (>) Greater than, (<) Less than (>=) Greater than or equal to (<=) Less than or equal to operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <!DOCTYPE html> <html> <head> <title>Comparison Operators in PHP</title> </head> <body> <?php $a = 3; $b = 2; $c = 3; $d = 4; $e = 4; if ($a > $b) { echo "a is greater then b<br/>"; } if ($a >= $c) { echo "a is greater then or equals to c<br/>"; } if ($a < $d) { echo "a is less then d<br/>"; } if ($e <= $d) { echo "a is less then or equals to d<br/>"; } ?> </body> </html> |
1 | a is greater then b<br>a is greater then or equals to c<br>a is less then d<br>a is less then or equals to d |
4. PHP Increment or Decrement Operator
Increment decrement operators are ++ and –.
++$x Pre-increment
$x++ Post-increment
–$x Pre-decrement
$x– Post-decrement
Example: Use of PHP (++) pre-increment , post increment , (–) pre decrement and post decrement operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <!DOCTYPE html> <html> <head> <title>Assignment Operators in PHP</title> </head> <body> <?php $a = 2; $b = 3; $c = 4; $d = 5; echo 'a = ' . ++$a . '<br/>'; echo 'b = ' . --$b . '<br/>'; echo 'c = ' . $c++ . '<br/>'; echo 'd = ' . $d-- . '<br/>'; echo 'c = ' . $c . '<br/>'; echo 'd = ' . $d . '<br/>'; ?> </body> </html> |
1 2 3 4 5 6 | a = 3 b = 2 c = 4 d = 5 c = 5 d = 4 |
5. PHP Logical Operators
and, or ,xor, &&, ||, ! are logical operators in PHP
And $x and $y
Or $x or $y
Xor $x xor $y
And $x && $y
Or $x || $y
Not !$x
Example: Use of PHP (and , &&) and operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!DOCTYPE html> <html> <head> <title>Logical Operators in PHP</title> </head> <body> <?php $a = 120; $b = 120; if ($a > 100 and $b > 100) { echo "Both Numbers are greater than 100<br/>"; } //insted of and we can use && if ($a > 100 && $b > 100) { echo "Both Numbers are greater than 100"; } ?> </body> </html> |
Result
1 | Both Numbers are greater than 100<br>Both Numbers are greater than 100 |
Example: Use of PHP (or , ||) and operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!DOCTYPE html> <html> <head> <title>Logical Operators in PHP</title> </head> <body> <?php $a = 120; $b = 50; if ($a > 100 or $b > 100) { echo "Either or both Numbers are greater than 100<br/>"; } //insted of and we can use && if ($a > 100 || $b > 100) { echo "Either or both Numbers are greater than 100"; } ?> </body> </html> |
1 2 | Either or both Numbers are greater than 100 Either or both Numbers are greater than 100 |
Example: Use of PHP (!) not and (xor) xor operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <!DOCTYPE html> <html> <head> <title>Logical Operators in PHP</title> </head> <body> <?php $a = 120; $b = 50; if ($a !== 100) { echo "A is not Equals to 100 <br/>"; } //work when one is true bu not both if ($a > 100 xor $b > 100) { echo "Hello xor"; } ?> </body> </html> |
1 | A is not Equals to 100<br>Hello xor |
6. PHP String Operators
. Concatenation $txt1 . $txt2
.= Concatenation assignment $txt1 .= $txt2
Example: Use of PHP (.) Concatenation and and (.=) Concatenation assignment operator.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!DOCTYPE html> <html> <head> <title>Logical Operators in PHP</title> </head> <body> <?php $str1 = "Hello "; $str2 = "PHP "; $str3 = "Learners"; $str4 = $str1 . $str2; $str4 .= $str3; echo $str4; ?> </body> </html> |
1 | Hello PHP Learners |
7. PHP Array Operators
It is used to compare arrays with each other
+ Union $x + $y
== Equality $x == $y
=== Identity $x === $y
!= Inequality $x != $y
<> Inequality $x <> $y
!== Non-identity $x !== $y
Example: Use of PHP array (+) union operator
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!DOCTYPE html> <html> <head> <title>Array Operators in PHP</title> </head> <body> <?php $a = array('0'=>1,'1'=> 2,'2'=> 3,'3'=> 4, '4'=>5); $b = array('5'=>6,'6'=> 7,'7'=> 8,'8'=> 9, '9'=>10); print_r($a + $b); ?> </body> </html> |
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 )
Example: Use of PHP array (==) Equality (===) Identity , (!=) Inequality (<>), Inequality and (!==) Non-identity and php (=>) operators.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <!DOCTYPE html> <html> <head> <title>Array Operators in PHP</title> </head> <body> <?php> $a = array('0' => 1, '1' => 2, '2' => 3, '3' => 4, '4' => 5); $b = array('5' => 6, '6' => 7, '7' => 8, '8' => 9, '9' => 10); $c = array('0' => 1, '1' => 2, '2' => 3, '3' => 4, '4' => 5); $d = array('0' => '1', '1' => '2', '2' => '3', '3' => '4', '4' => '5'); echo '1 var_dump($a == $b) '; var_dump($a == $b); echo '<br/>'; echo '2 var_dump($a != $b) '; var_dump($a != $b); echo '<br/>'; echo '3 var_dump($a <> $b) '; var_dump($a <> $b); echo '<br/>'; echo '4 var_dump($a == $c) '; var_dump($a == $c); echo '<br/>'; echo '5 var_dump($a == $d) '; var_dump($a == $d); echo '<br/>'; echo '6 var_dump($a === $d) '; var_dump($a === $d); echo '<br/>'; echo '7 var_dump($a === $c) '; var_dump($a === $c); echo '<br/>'; ?> </body> </html> |
Output
1 2 3 4 5 6 7 | 1 var_dump($a == $b) bool(false) 2 var_dump($a != $b) bool(true) 3 var_dump($a <> $b) bool(true) 4 var_dump($a == $c) bool(true) 5 var_dump($a == $d) bool(true) 6 var_dump($a === $d) bool(false) 7 var_dump($a === $c) bool(true) |