## 07 Jul PHP Operators

PHP Operators perform operations by taking one or more values, to give another value. These operations are performed on variables and values.

For example: $a + $b, $a – $b, $a = $b

The types of operators are explained here with an example, where **$a = 5**, **$b =10**. The following are the operators in PHP:

- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Increment/ Decrement Operators
- String Operators

## PHP Arithmetic operators

Arithmetic operators perform arithmetical operations, such as addition, subtraction, division, multiplication, etc.

Operator | Name | Example |
---|---|---|

+ | Addition | $a + $b |

- | Subtraction | $a - $b |

* | Multiplication | $a * $b |

/ | Division | $a / $b |

% | Modulus | $a % $b |

** | Exponentiation | $a ** $b |

## PHP Assignment operators

Use the Assignment Operator when you need to assign a value to a variable. Assignment means assigning the value of the right operand(s) to the left operand.

Operator | Name | Example |
---|---|---|

= | Assignment | $a = $b |

+= | Add AND assignment | $a += $b is same as $a = $a + $b |

-= | Subtract AND assignment | $a -= $b is same as $a = $a - $b |

*= | Multiply AND assignment | $a *= $b is same as $a = $a * $b |

/= | Divide AND assignment | $a /= $b is same as $a = $a / $b |

%= | Modulus AND assignment | $a % $b is same as $a = $a % $b |

## PHP Relational operators

Compare two values with PHP relational operators.

Operator | Name | Example |
---|---|---|

== | Equal | $a == $b isn’t true |

!= | Not Equal | $a != $b returns true |

> | Greater Than | $a > $b isn’t true |

< | Less Than | $a < $b is true |

>= | Greater than or equal to | $a >= $b isn’t true |

<= | Less than or equal to | $a <= $b is true |

## PHP Logical operators

Logical operators combine conditional statements.

Operator | Name | Example |
---|---|---|

And | Logical AND operator | $a and $b is true if both $a and $b are true |

Or | Logical OR operator | $a or $b is true either $a or $b is true |

Xor | Logical XOR operator | $a xor $b is true if either $a or $b is true (not both) |

&& | Logical AND operator | $a && $b is true if $a and $b are both true |

|| | Logical OR Operator | $a || $b is true if either $a or $b is true |

! | Logical NOT Operator | !$a is true if $a is not true |

## PHP Increment/ Decrement operators

Increment and decrement a variable’s value with Increment and Decrement operators respectively.

Operator | Name | Example |
---|---|---|

++$a | Increment (Pre) | $a increments, then returns |

$a++ | Increment (Post) | Returns $a, then increments |

--$a | Decrement (Pre) | $a decrements, then returns |

$a-- | Decrement (Post) | Returns $a, then decrements |

## PHP String operators

Here are the operators for strings. Let’s say, we have the following two strings,

1 2 3 4 |
$str1 = "Cricket"; $str2 = "World"; |

The following are the string operators:

Operator | Name | Example |
---|---|---|

. | Concatenation | $str1 . $str2 |

.= | Concatenation assignment | $str1 .= $str2 |

## No Comments