PHP Operators

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.




Arithmetic Operators in PHP


';
echo 'sub = ' . $sub . '
'; echo 'mul = ' . $mul . '
'; echo 'div = ' . $div . '
'; echo 'mod = ' . $mod . '
'; echo 'pow = ' . $pow . '
'; ?>

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 (=, +=, -=, *=, /=, %=, **=)




Assignment Operators in PHP


';
echo 'd = ' . $d . '
'; echo 'e = ' . $e . '
'; echo 'f = ' . $f . '
'; echo 'g = ' . $g . '
'; echo 'h = ' . $h . '
'; echo 'i = ' . $i . '
'; ?>

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.




Comparison Operators in PHP


";
}
if ($a == $c) {
echo "Both are equal
"; } if ($a === $c) { echo "Both are identical
"; } else{ echo "Both are not identical"; } ?>
Both are equal
Both are equal
Both are not identical

Example: Use of PHP (!=) Not equal and  (<>) Not equal  and (!==) Not identical operators.




Comparison Operators in PHP


";
}
if ($a <> $c) {
echo "Both are not equal
"; } if ($a !== $d) { echo "Both are not identical
"; } ?>
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.




Comparison Operators in PHP


 $b) {
echo "a is greater then b
"; } if ($a >= $c) { echo "a is greater then or equals to c
"; } if ($a < $d) { echo "a is less then d
"; } if ($e <= $d) { echo "a is less then or equals to d
"; } ?>
a is greater then b
a is greater then or equals to c
a is less then d
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.




Assignment Operators in PHP


';
echo 'b = ' . --$b . '
'; echo 'c = ' . $c++ . '
'; echo 'd = ' . $d-- . '
'; echo 'c = ' . $c . '
'; echo 'd = ' . $d . '
'; ?>
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.




Logical Operators in PHP


 100 and $b > 100) {
echo "Both Numbers are greater than 100
"; } //insted of and we can use && if ($a > 100 && $b > 100) { echo "Both Numbers are greater than 100"; } ?>

Result

Both Numbers are greater than 100
Both Numbers are greater than 100

Example: Use of PHP  (or , ||) and operator.




Logical Operators in PHP


 100 or $b > 100) {
echo "Either or both Numbers are greater than 100
"; } //insted of and we can use && if ($a > 100 || $b > 100) { echo "Either or both Numbers are greater than 100"; } ?>
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.




Logical Operators in PHP


";
}
//work when one is true bu not both
if ($a > 100 xor $b > 100) {
echo "Hello xor";
}
?>

A is not Equals to 100
Hello xor

6. PHP String Operators

. Concatenation $txt1 . $txt2
.= Concatenation assignment $txt1 .= $txt2

Example: Use of PHP  (.) Concatenation and and (.=) Concatenation assignment operator.




Logical Operators in PHP




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




Array Operators in PHP


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);
?>

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.



    
        Array Operators in 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 '
'; echo '2 var_dump($a != $b) '; var_dump($a != $b); echo '
'; echo '3 var_dump($a <> $b) '; var_dump($a <> $b); echo '
'; echo '4 var_dump($a == $c) '; var_dump($a == $c); echo '
'; echo '5 var_dump($a == $d) '; var_dump($a == $d); echo '
'; echo '6 var_dump($a === $d) '; var_dump($a === $d); echo '
'; echo '7 var_dump($a === $c) '; var_dump($a === $c); echo '
'; ?>

Output

 
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)