# PHP Operators

An operator is a symbol that specifies an action to perform in an expression, for example, the '+' symbol in the expression '1 + 2' would add the two numbers together to produce a result. This type of operator is known as an arithmetic operator, however, there are also other groups of operators within PHP that will be explained below.

## Arithmetic Operators

Arithmetic operators allow for mathematical calculations to be carried out within a PHP script.

Operator |
Description |

+ | Adds two values together. |

- | Subtracts one value from another. |

* | Multiplies two values together. |

/ | Divides one value by another. |

% | Calculates the remainder after one value is divided by another. |

The following examples show each of these operators in action.

// Variables to use in expressions. $a = 10; $b = 20; $c = 0; $c = $a + $b; // c will equal 30. $c = $b - $a; // c will equal 10. $c = $a * $b; // c will equal 200. $c = $b / $a; // c will equal 2. $c = $b % $a; // c will equal 0.

## Assignment Operators

Assignment Operators in PHP are used to assign values to a variable, the most basic of which is '='. Here, the '=' symbol means 'gets set to' rather than 'equals', so in the expression '$x = $y', 'x' gets set to 'y'. The Arithmetic Operator symbols can be combined with '=' to form the Assignment Operators used in the following examples.

$a = 10; // a gets set to 10. $b = 2; $b += 5; // b gets set to 7. $c = 10; $c -= 4; // c gets set to 6. $d = 2; $d *= 2; // d gets set to 4. $e = 20; $e /= 5; // e gets set to 4. $f = 5; $f %= 2; // f gets set to 1.

## String Operators

Operator |
Description |

. | Concatenation operator that combines two items into a single string. |

.= | Concatenation assignment operator that adds the string on the right to the end of the one on the left. |

Below are examples of these two string operators, both of which produce the same result, a single string containing 'Hello world'.

$a = "Hello"; $b = $a . " world"; $x = "Hello"; $x .= " world";

## Relational Operators

Relational operators compare two values and are used, for example, in 'if' statements, which are described in the next section on Decision Making. The result of the comparison is either True or False.

Operator |
Description |

== | Evaluates to True if the two values are equal. |

=== | Evaluates to True if the two values are identical in value and type. |

!= | Evaluates to True if the two values are not equal. |

<> | Evaluates to True if the two values are not equal. |

!== | Evaluates to True if the two values are not identical in value or of the same type. |

> | Evaluates to True if the first value is greater than the second. |

< | Evaluates to True if the first value is less than the second. |

>= | Evaluates to True if the first value is greater than or equal to the second. |

<= | Evaluates to True if the first value is less than or equal to the second. |

## Logical Operators

Logical operators are used where you want to test more than one condition at the same time, for example, in an 'if' statement.

Operator |
Description |

and | Evaluates to True if both conditions are True. |

or | Evaluates to True if either condition is True. |

xor | Evaluates to True if either, but not both conditions are True. |

&& | Evaluates to True if both conditions are True. |

|| | Evaluates to True if either condition is True. |