An operator is something that takes one or more values (or expressions, in programming jargon) and yields another value (so that the construction itself becomes an expression). Operators can be assembled by the quantity of qualities they take. Unary Operators take just a single esteem, for instance ! (the legitimate not Operators) or ++ (the augmentation Operators).

Binary Operators take two esteems, for example, the commonplace arithmetical Operators + (in addition to) and – (minus), and the greater part of PHP Operators fall into this class. At long last, there is a solitary ternary Operators, ? :, which takes three esteems; this is generally alluded to just as “the ternary administrator” (in spite of the fact that it could maybe more appropriately be known as the contingent Operators).

Below list will help php programming using Operators

## PHP Operators – Manual: Table of Contents

- PHP Arithmetic Operators
- PHP Assignment Operators
- PHP Bitwise Operators
- PHP Comparison Operators
- PHP Incrementing/Decrementing Operators
- PHP Logical Operators
- PHP String Operators
- PHP Array Operators

## PHP Arithmetic Operators

Example | Name | Result |
---|---|---|

+$a | Identity | Conversion of $a to int orfloat as appropriate. |

-$a | Negation | Opposite of $a. |

$a + $b | Addition | Sum of $a and $b. |

$a – $b | Subtraction | Difference of $a and $b. |

$a * $b | Multiplication | Product of $a and $b. |

$a / $b | Division | Quotient of $a and $b. |

$a % $b | Modulo | Remainder of $a divided by $b. |

$a ** $b | Exponentiation | Result of raising $a to the $b‘th power. Introduced in PHP 5.6. |

Demo

```
<?php
echo (5 % 3)."\n"; // prints 2
echo (5 % -3)."\n"; // prints 2
echo (-5 % 3)."\n"; // prints -2
echo (-5 % -3)."\n"; // prints -2
?>
```

## PHP Assignment Operators

Assignment | Same as… | Description |
---|---|---|

x = y | x = y | The left operand gets set to the value of the expression on the right |

x += y | x = x + y | Addition |

x -= y | x = x – y | Subtraction |

x *= y | x = x * y | Multiplication |

x /= y | x = x / y | Division |

x %= y | x = x % y | Modulus |

Demo

```
<?php
$x = 10;
echo $x;
// Output 10
$x = 20;
$x += 100;
echo $x;
// Output 120
$x = 50;
$x -= 30;
echo $x;
// Output 20
$x = 10;
$y = 6;
echo $x * $y;
// Output 60
$x = 10;
$x /= 5;
echo $x;
// Output 2
$x = 15;
$x %= 4;
echo $x;
// Output 3
?>
```

[ads1]

## PHP Bitwise Operators

Example | Name | Result |
---|---|---|

`$a & $b` | And | Bits that are set in both $a and $b are set. |

`$a | $b` | Or (inclusive or) | Bits that are set in either $a or $b are set. |

`$a ^ $b` | Xor (exclusive or) | Bits that are set in $a or $b but not both are set. |

`~ $a` | Not | Bits that are set in $a are not set, and vice versa. |

`$a << $b` | Shift left | Shift the bits of $a $b steps to the left (each step means “multiply by two”) |

`$a >> $b` | Shift right | Shift the bits of $a $b steps to the right (each step means “divide by two”) |

## PHP Comparison Operators

Example | Name | Result |
---|---|---|

$a == $b | Equal | if `TRUE` $a is equal to $b after type juggling. |

$a === $b | Identical | if `TRUE` $a is equal to $b, and they are of the sametype. |

$a != $b | Not equal | if `TRUE` $a is not equal to $b after type juggling. |

$a <> $b | Not equal | if `TRUE` $a is not equal to $b after type juggling. |

$a !== $b | Not identical | if `TRUE` $a is not equal to $b, or they are not of the sametype. |

$a < $b | Less than | if `TRUE` $a is strictly less than $b. |

$a > $b | Greater than | if `TRUE` $a is strictly greater than $b. |

$a <= $b | Less than or equal to | if `TRUE` $a is less than or equal to $b. |

$a >= $b | Greater than or equal to | if `TRUE` $a is greater than or equal to $b. |

$a <=> $b | Spaceship | An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b. Available as of PHP 7. |

## PHP Increment/decrement Operators

Example | Name | Effect |
---|---|---|

++$a | Pre-increment | Increments $a by one, then returns $a. |

$a++ | Post-increment | Returns $a, then increments $a by one. |

–$a | Pre-decrement | Decrements $a by one, then returns $a. |

$a– | Post-decrement | Returns $a, then decrements $a by one. |

Demo

```
<?php
echo "<h3>Postincrement</h3>";
$a = 5;
echo "Should be 5: " . $a++ . "<br />\n";
echo "Should be 6: " . $a . "<br />\n";
echo "<h3>Preincrement</h3>";
$a = 5;
echo "Should be 6: " . ++$a . "<br />\n";
echo "Should be 6: " . $a . "<br />\n";
echo "<h3>Postdecrement</h3>";
$a = 5;
echo "Should be 5: " . $a-- . "<br />\n";
echo "Should be 4: " . $a . "<br />\n";
echo "<h3>Predecrement</h3>";
$a = 5;
echo "Should be 4: " . --$a . "<br />\n";
echo "Should be 4: " . $a . "<br />\n";
?>
```

## PHP Logical Operators

Example | Name | Result |
---|---|---|

$a and $b | And | if both `TRUE` $a and $b are .`TRUE` |

$a or $b | Or | if either `TRUE` $a or $b is .`TRUE` |

$a xor $b | Xor | if either `TRUE` $a or $b is , but not both.`TRUE` |

! $a | Not | if `TRUE` $a is not .`TRUE` |

$a && $b | And | if both `TRUE` $a and $b are .`TRUE` |

$a || $b | Or | if either `TRUE` $a or $b is .`TRUE` |

Demo Code

```
<?php
// --------------------
// foo() will never get called as those operators are short-circuit
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());
// --------------------
// "||" has a greater precedence than "or"
// The result of the expression (false || true) is assigned to $e
// Acts like: ($e = (false || true))
$e = false || true;
// The constant false is assigned to $f before the "or" operation occurs
// Acts like: (($f = false) or true)
$f = false or true;
var_dump($e, $f);
// --------------------
// "&&" has a greater precedence than "and"
// The result of the expression (true && false) is assigned to $g
// Acts like: ($g = (true && false))
$g = true && false;
// The constant true is assigned to $h before the "and" operation occurs
// Acts like: (($h = true) and false)
$h = true and false;
var_dump($g, $h);
?>
```

The above example will output something similar to:

```
bool(true)
bool(false)
bool(false)
bool(true)
```

## PHP String Operators

There are two string operators. The first is the concatenation operator (‘.’), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (‘.=’), which appends the argument on the right side to the argument on the left side. Please read Assignment Operators for more information.

```
<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"
$a = "Hello ";
$a .= "World!"; // now $a contains "Hello World!"
?>
```

## PHP Array Operators

Example | Name | Result |
---|---|---|

$a + $b | Union | Union of $a and $b. |

$a == $b | Equality | if `TRUE` $a and $b have the same key/value pairs. |

$a === $b | Identity | if `TRUE` $a and $b have the same key/value pairs in the sameorder and of the same types. |

$a != $b | Inequality | if `TRUE` $a is not equal to $b. |

$a <> $b | Inequality | if `TRUE` $a is not equal to $b. |

$a !== $b | Non-identity | if `TRUE` $a is not identical to $b. |

The + operator returns the right-hand array appended to the left-hand array; for keys that exist in both arrays, the elements from the left-hand array will be used, and the matching elements from the right-hand array will be ignored.

```
<?php
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
$c = $a + $b; // Union of $a and $b
echo "Union of \$a and \$b: \n";
var_dump($c);
$c = $b + $a; // Union of $b and $a
echo "Union of \$b and \$a: \n";
var_dump($c);
$a += $b; // Union of $a += $b is $a and $b
echo "Union of \$a += \$b: \n";
var_dump($a);
?>
```

When executed, this script will print the following:

```
Union of $a and $b:
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(6) "cherry"
}
Union of $b and $a:
array(3) {
["a"]=>
string(4) "pear"
["b"]=>
string(10) "strawberry"
["c"]=>
string(6) "cherry"
}
Union of $a += $b:
array(3) {
'a' =>
string(5) "apple"
'b' =>
string(6) "banana"
'c' =>
string(6) "cherry"
}
```

## Leave a Reply