Operators are symbols used to perform operations on variables and values. PHP supports arithmetic, assignment, comparison, logical, increment/decrement, string, array and other operators.
Arithmetic operators perform mathematical calculations.
| Operator | Meaning | Example |
|---|---|---|
| + | Addition | 10 + 5 = 15 |
| - | Subtraction | 10 - 5 = 5 |
| * | Multiplication | 10 * 5 = 50 |
| / | Division | 10 / 5 = 2 |
| % | Modulus (remainder) | 10 % 3 = 1 |
| ** | Exponentiation | 2 ** 3 = 8 |
Example:
<?php
$a = 10;
$b = 3;
echo $a + $b;
echo "<br>";
echo $a - $b;
echo "<br>";
echo $a * $b;
echo "<br>";
echo $a / $b;
echo "<br>";
echo $a % $b;
echo "<br>";
echo $a ** $b;
?>
Output:
13
7
30
3.3333333333333
1
1000
Remember: the modulus operator returns the remainder after division.
Assignment operators assign values to variables or update their existing values.
| Operator | Example | Equivalent operation |
|---|---|---|
| = | $x = 10 | Assign 10 to $x |
| += | $x += 5 | $x = $x + 5 |
| -= | $x -= 5 | $x = $x - 5 |
| *= | $x *= 2 | $x = $x * 2 |
| /= | $x /= 2 | $x = $x / 2 |
| %= | $x %= 3 | $x = $x % 3 |
Example:
<?php
$x = 10;
$x += 5;
echo $x;
echo "<br>";
$x -= 3;
echo $x;
echo "<br>";
$x *= 2;
echo $x;
?>
Output:
15
12
24
Comparison operators compare two values. They generally return a Boolean result: true or false.
| Operator | Meaning |
|---|---|
| == | Equal in value after type conversion if needed |
| === | Identical in value and type |
| != | Not equal |
| !== | Not identical in value or type |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
Example:
<?php
$a = 10;
$b = "10";
var_dump($a == $b);
echo "<br>";
var_dump($a === $b);
echo "<br>";
var_dump($a != 5);
echo "<br>";
var_dump($a > 5);
?>
Output:
bool(true)
bool(false)
bool(true)
bool(true)
The == operator compares values with
possible type conversion. The ===
operator also requires the types to match.
Logical operators combine or reverse conditions. They are commonly used with if statements.
| Operator | Meaning |
|---|---|
| && | True when both conditions are true |
| || | True when at least one condition is true |
| ! | Reverses a Boolean value |
| and | Logical AND with lower precedence than && |
| or | Logical OR with lower precedence than || |
| xor | True when exactly one operand is true |
Example using &&:
<?php
$age = 20;
$hasId = true;
if ($age >= 18 && $hasId) {
echo "Entry allowed";
} else {
echo "Entry denied";
}
?>
Output:
Entry allowed
Example using ||:
<?php
$day = "Sunday";
if ($day == "Saturday" || $day == "Sunday") {
echo "Holiday";
} else {
echo "Working day";
}
?>
Output:
Holiday
Increment operators increase a variable's value by one. Decrement operators decrease it by one.
| Operator | Meaning |
|---|---|
| ++$x | Increment first, then use the value |
| $x++ | Use the value first, then increment |
| --$x | Decrement first, then use the value |
| $x-- | Use the value first, then decrement |
Example:
<?php
$x = 5;
echo ++$x;
echo "<br>";
echo $x++;
echo "<br>";
echo $x;
echo "<br>";
echo --$x;
?>
Output:
6
6
7
6
Prefix changes the value before using it. Postfix uses the current value before changing it.
PHP provides the dot operator (.) to
concatenate strings, meaning to join them together.
Example:
<?php
$firstName = "Rahul";
$lastName = "Kumar";
$fullName = $firstName . " " . $lastName;
echo $fullName;
?>
Output:
Rahul Kumar
The .= operator appends text to an
existing string.
<?php
$message = "Welcome";
$message .= " to PHP";
echo $message;
?>
Output:
Welcome to PHP
The ternary operator is a short way to choose between two values based on a condition.
Syntax:
condition ? value_if_true : value_if_false;
Example:
<?php
$marks = 75;
$result = ($marks >= 40) ? "Pass" : "Fail";
echo $result;
?>
Output:
Pass
If the condition is true, the first value is selected. Otherwise, the second value is selected.
Null coalescing operator (??)
It returns the left-hand value if it exists and is not null; otherwise, it returns the right-hand value.
<?php
$username = null;
$name = $username ?? "Guest";
echo $name;
?>
Output:
Guest
Spaceship operator (<=>)
It compares two values and returns -1, 0, or 1.
<?php
echo 5 <=> 10;
echo "<br>";
echo 10 <=> 10;
echo "<br>";
echo 15 <=> 10;
?>
Output:
-1
0
1
A negative result means the left value is smaller, zero means the values compare equal, and a positive result means the left value is greater.
PHP also supports operators for comparing and combining arrays.
Array union (+):
<?php
$a = ["name" => "Rahul", "age" => 20];
$b = ["city" => "Patna", "age" => 25];
$result = $a + $b;
print_r($result);
?>
Output:
Array
(
[name] => Rahul
[age] => 20
[city] => Patna
)
The array union operator keeps all keys from the left-hand array. When a key exists in both arrays, the left-hand value is retained.
Operator precedence:
Operator precedence determines which operation is performed first. Parentheses can make the intended order clear.
<?php
$result1 = 5 + 3 * 2;
$result2 = (5 + 3) * 2;
echo $result1;
echo "<br>";
echo $result2;
?>
Output:
11
16
Multiplication is performed before addition in the first expression. Parentheses change the order in the second expression.
Write PHP programs to complete these exercises:
Sample starting code:
<?php
$a = 25;
$b = 4;
echo "Addition: " . ($a + $b);
echo "<br>";
echo "Remainder: " . ($a % $b);
echo "<br>";
$marks = 65;
$result = ($marks >= 40) ? "Pass" : "Fail";
echo $result;
?>
1. Which operator is used for exponentiation in PHP?
2. What is the result of 10 % 3?
3. Which operator joins strings in PHP?