7. Operators: Arithmetic, Assignment, and Comparison
Operators are used to perform operations on values and variables.
1. Arithmetic Operators
Used for mathematical calculations:
| Operator | Name | Example |
|---|---|---|
+ | Addition | $a + $b |
- | Subtraction | $a - $b |
* | Multiplication | $a * $b |
/ | Division | $a / $b |
% | Modulus (Remainder) | $a % $b |
** | Exponentiation | $a ** $b (a raised to the power of b) |
php
2. Assignment Operators
Used to assign values to variables. The basic assignment operator is = .
| Operator | Example | Same as |
|---|---|---|
= | $x = 10 | Assigns 10 to x |
+= | $x += 5 | $x = $x + 5 |
*= | $x *= 2 | $x = $x * 2 |
.= | $str .= 'world' | $str = $str . 'world' (String Concatenation) |
3. Comparison Operators
Used to compare two values. They return a boolean (true or false).
| Operator | Name | Description |
|---|---|---|
== | Equal | Checks if values are equal (loose comparison) |
=== | Identical | Checks if values AND data types are equal (strict comparison) |
!= | Not equal | Checks if values are not equal |
<> | Not equal | Alternative to != |
!== | Not identical | Checks if values OR data types are not equal |
> | Greater than | |
< | Less than | |
>= | Greater than or equal to | |
<= | Less than or equal to |
We will use comparison operators extensively when covering conditional statements.