## PHP Operators

An operator is a symbol that manipulates one or more values, usually producing a new value in the process. Meanwhile, an expression in PHP is anything that evaluates to a value; this can be any combination of values, variables, operators, and functions. In the preceding example, $x + $y is an expression. Here are some more examples of expressions:

$x + $y + $z

$x -$y

$x 5

true

gettype( $test_var ) The values and variables that are used with an operator are known as operands .

**Operator Types**

Operators in PHP can be grouped into ten types, as follows:

Type | Description |
---|---|

Arithmetic | Perform common arithmetical operations such as addition and subtraction |

Assignment | Assign values to variables |

Bitwise | Perform operations on individual bits in an integer |

Comparison | Compare values in a Boolean fashion ( true or false is returned) |

Error | Control Affect error handling |

Execution | Cause execution of commands as though they were shell commands |

Incrementing/Decrementing | Increment or decrement a variable ’ s value |

Logical | Boolean operators such as and or and not that can be used to include or exclude |

String | Concatenates (joins together) strings (there ’ s only one string operator) |

Array | Perform operations on arrays |

**The Assignment Operator**

You have met the assignment operator each time we have initialized a variable. It consists of the single character =. The assignment operator takes the value of its right-hand operand and assigns it to its left-hand operand:

$name =”matt”;

The variable $name now contains the string “matt”. Interestingly, this construct is an expression. It might look at first glance that the assignment operator simply changes the variable $name without producing a value, but in fact, a statement that uses the assignment operator always resolves to a copy of the value of the right operand. Thus print ( $name = “matt” );

prints the string “matt” to the browser in addition to assigning “matt” to $name.

For example,

$x = 4;

$x += 4; // $x now equals 8 is equivalent to $x = 4;

$x = $x + 4; // $x now equals 8

There is an assignment operator for each of the arithmetic operators and one for the concatenation operator.

Operator | Example | Equivalent to |
---|---|---|

+= | $x += 5 | $x = $x + 5 |

− = | $x − = 5 | $x = $x − 5 |

/= | $x /= 5 | $x = $x / 5 |

*= | $x *= 5 | $x = $x *5 |

%= | $x%=5 | $x = $x % 5 |

.= | $x .= “test” | $x = $x” test” |

**Arithmetic Operators**

The arithmetic operators do exactly what you would expect. The addition operator adds the right operand to the left operand. The subtraction operator subtracts the right-hand operand from the left. The division operator divides the left-hand operand by the right. The multiplication operator multiplies the left-hand operand by the right. The modulus operator returns the remainder of the left operand divided by the right.

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

+ | Addition | 10+3 | 13 |

− | Subtraction | 10−3 | 7 |

/ | Division | 10/3 | 3.3333333333333 |

* | Multiplication | 10*3 | 30 |

% | Modulus | 10%3 | 1 |

**The Concatenation Operator**

The concatenation operator is a single dot. Treating both operands as strings, it appends the right-hand operand to the left. So