Operators are symbols used to perform operations on variables and values. Understanding how to use operators is essential for performing calculations, comparisons, and logic within your PHP scripts.
PHP groups operators into several major categories.
1. Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
| Operator | Name | Description | Example | Result |
+ | Addition | Sum of values | $a = 10 + 5; | $a is 15 |
- | Subtraction | Difference between values | $b = 10 - 5; | $b is 5 |
* | Multiplication | Product of values | $c = 10 * 5; | $c is 50 |
/ | Division | Quotient of values | $d = 10 / 5; | $d is 2 |
% | Modulus | Remainder of a division | $e = 10 % 3; | $e is 1 |
** | Exponentiation | $a raised to the power of $b | $f = 2 ** 3; | $f is 8 |
Example:
<?php
$num1 = 15;
$num2 = 4;
echo "Addition: " . ($num1 + $num2) . "<br>"; // Output: 19
echo "Remainder: " . ($num1 % $num2); // Output: 3
?>
2. Assignment Operators
Assignment operators are used to set the value of a variable. The basic assignment operator is =, which assigns the value of the right operand to the left operand. Compound assignment operators perform an operation and an assignment simultaneously.
| Operator | Example | Same As | Description |
= | $x = 10; | – | Assigns the value 10 to $x. |
+= | $x += 5; | $x = $x + 5; | Adds 5 to the current value of ` $x$. |
*= | $x \*= 2; | $x = $x * 2; | Multiplies $xby 2. |
Example:
<?php
$counter = 5;
$counter += 3; // $counter is now 8
$message = "Hello";
$message .= " PHP!"; // $message is now "Hello PHP!"
echo $counter . " " . $message;
?>
3. Comparison Operators
Comparison operators are used to compare two values ($x and $y). They always return a Boolean value (TRUE or FALSE).
| Operator | Name | Description | Example | Returns TRUE if… |
== | Equal | Checks if values are equal (ignores data type). | $x == $y | $x is equal to $y. |
=== | Identical | Checks if values and data types are equal. | $x === $y | $x and $y have the same value AND type. |
!= | Not Equal | Checks if values are not equal. | $x != $y | $x is not equal to ` $y$. |
<= | Less Than or Equal To | Checks if the left operand is smaller or equal. | $x <= $y | $x is less than or equal to `$y$. |
Example (Value vs. Identity):
<?php
$a = 5; // Integer
$b = "5"; // String
// Checks only the VALUE
var_dump($a == $b); // Output: bool(true)
// Checks VALUE and DATA TYPE
var_dump($a === $b); // Output: bool(false)
?>
4. Logical Operators
Logical operators are used to combine conditional statements (Booleans). They are essential for if/else control structures (covered in the next chapter).
| Operator | Name | Description | Example |
and or && | AND | Returns TRUE if both conditions are TRUE. | $a && $b |
or or ` | ` | OR | |
xor | XOR | Returns TRUE if one condition is TRUE, but not both. | $a xor $b |
! | NOT | Reverses the result; TRUE becomes FALSE, and vice versa. | !$a |
Example:
<?php
$age = 20;
$is_member = true;
// Is the user over 18 AND a member?
if ($age > 18 && $is_member) {
echo "Access Granted!";
}
?>
Next Steps
Operators allow you to make comparisons and calculate values. In the next crucial chapter, we will use these comparison and logical operators to control what code runs and when using “PHP Conditional Statements (if...else)”.
