About Lesson
Operators in PHP
Operators are special symbols or keywords that are used to perform operations on variables and values.
In PHP, operators are grouped into categories: Arithmetic, Assignment, Comparison, and Logical.
1. Arithmetic Operators:
These operators are used to perform mathematical calculations.
Operator | Description | Example | Result |
---|---|---|---|
+ | Addition | 10 + 5 | 15 |
- | Subtraction | 10 - 5 | 5 |
* | Multiplication | 10 * 5 | 50 |
/ | Division | 10 / 5 | 2 |
% | Modulus (remainder) | 10 % 3 | 1 |
** | Exponentiation (power) | 2 ** 3 | 8 |
Example:
PHP
<!DOCTYPE html>
<html>
<head>
<title>Arithmetic Operators</title>
</head>
<body>
<?php
$a = 10;
$b = 3;
echo "<h2>Arithmetic Operators</h2>";
echo "a + b = " . ($a + $b) . "<br>";
echo "a - b = " . ($a - $b) . "<br>";
echo "a * b = " . ($a * $b) . "<br>";
echo "a / b = " . ($a / $b) . "<br>";
echo "a % b = " . ($a % $b) . "<br>";
echo "a ** b = " . ($a ** $b) . "<br>";
?>
</body>
</html>
Explanation:
$a + $b
adds two numbers.$a % $b
gives remainder when$a
is divided by$b
.$a ** $b
means$a
raised to the power$b
.
2. Assignment Operators:
Assignment operators are used to assign values to variables.
Operator | Example | Same as |
---|---|---|
= | $x = 5 | Assigns 5 to $x |
+= | $x += 3 | $x = $x + 3 |
-= | $x -= 3 | $x = $x - 3 |
*= | $x *= 3 | $x = $x * 3 |
/= | $x /= 3 | $x = $x / 3 |
%= | $x %= 3 | $x = $x % 3 |
Example:
PHP
<?php
$x = 10;
echo "<h2>Assignment Operators</h2>";
echo "Initial value: $x <br>";
$x += 5;
echo "After x += 5: $x <br>";
$x -= 3;
echo "After x -= 3: $x <br>";
$x *= 2;
echo "After x *= 2: $x <br>";
$x /= 4;
echo "After x /= 4: $x <br>";
$x %= 3;
echo "After x %= 3: $x <br>";
?>
Explanation:
$x += 5
increases$x
by 5.$x *= 2
multiplies$x
by 2.$x %= 3
gives remainder after dividing$x
by 3.
3. Comparison Operators:
These are used to compare two values. They return true
or false
.
Operator | Description | Example | Result |
---|---|---|---|
== | Equal to | 5 == "5" | true |
=== | Identical (equal + same data type) | 5 === "5" | false |
!= or <> | Not equal | 5 != 3 | true |
!== | Not identical | 5 !== "5" | true |
> | Greater than | 10 > 5 | true |
< | Less than | 10 < 5 | false |
>= | Greater than or equal | 10 >= 10 | true |
<= | Less than or equal | 10 <= 5 | false |
<=> | Spaceship (returns -1, 0, 1) | 10 <=> 20 | -1 |
Example:
PHP
<?php
$a = 10;
$b = "10";
echo "<h2>Comparison Operators</h2>";
var_dump($a == $b); // true
echo "<br>";
var_dump($a === $b); // false
echo "<br>";
var_dump($a != $b); // false
echo "<br>";
var_dump($a !== $b); // true
echo "<br>";
var_dump($a > 5); // true
echo "<br>";
var_dump($a < 5); // false
echo "<br>";
var_dump($a <=> 20); // -1
?>
Explanation:
==
checks only value.===
checks value + data type.<=>
is a spaceship operator: returns-1
if left < right,0
if equal,1
if left > right.
4. Logical Operators:
These are used to combine multiple conditions.
Operator | Description | Example | Result |
---|---|---|---|
&& or and | True if both are true | ($x > 5 && $x < 15) | true |
` | or or` | True if any is true | |
! | Not (negation) | !($x > 5) | false |
xor | True if one is true but not both | (true xor false) | true |
Example:
PHP
<?php
$x = 10;
echo "<h2>Logical Operators</h2>";
if ($x > 5 && $x < 15) {
echo "x is between 5 and 15 <br>";
}
if ($x > 5 || $x < 5) {
echo "x is greater than 5 OR less than 5 <br>";
}
if (!($x < 5)) {
echo "x is NOT less than 5 <br>";
}
if (true xor false) {
echo "One condition is true, but not both <br>";
}
?>
Explanation:
&&
checks if both conditions are true.||
checks if at least one condition is true.!
reverses the condition.xor
is true only when exactly one condition is true.