Objective: Understand how PHP performs mathematical calculations, logical comparisons,
and combines conditions to control program behavior.
Operators are symbols that tell PHP what action to perform on values or variables. They’re the
verbs of programming — they add, compare, or decide.
There are four main categories:
Used for basic math operations.
| Operator | Description | Example | Result |
|---|---|---|---|
+ | Addition | $x + $y | Sum |
- | Subtraction | $x - $y | Difference |
* | Multiplication | $x * $y | Product |
/ | Division | $x / $y | Quotient |
% | Modulus | $x % $y | Remainder |
$x = 10;
$y = 3;
echo $x + $y; // 13
echo $x % $y; // 1These store and update variable values.
| Operator | Example | Equivalent To | Description |
|---|---|---|---|
= | $x = 5 | — | Assign value |
+= | $x += 3 | $x = $x + 3 | Add & assign |
-= | $x -= 2 | $x = $x - 2 | Subtract & assign |
*= | $x *= 2 | $x = $x * 2 | Multiply & assign |
/= | $x /= 2 | $x = $x / 2 | Divide & assign |
🧠 Shortcut tip:
$count = 1;
$count += 4; // $count is now 5Used to compare two values and return true or false.
| Operator | Description | Example | Result |
|---|---|---|---|
== | Equal | $x == $y | true if values are equal |
=== | Identical | $x === $y | true if values and types are equal |
!= | Not equal | $x != $y | true if values are not equal |
!== | Not identical | $x !== $y | true if values or types differ |
> | Greater than | $x > $y | true if x > y |
< | Less than | $x < $y | true if x < y |
>= | Greater or equal | $x >= $y | true if x ≥ y |
<= | Less or equal | $x <= $y | true if x ≤ y |
$a = 10;
$b = "10";
var_dump($a == $b); // true
var_dump($a === $b); // false (different types)Used to combine multiple conditions.
| Operator | Meaning | Example | Result |
|---|---|---|---|
&& | AND | $x > 5 && $x < 15 | true if both conditions are true |
|| | OR | $x < 5 || $x > 15 | true if at least one condition is true |
! | NOT | !($x > 5) | reverses the condition |
🧠 Use these inside if-statements to control logic.
Used for counting loops and math logic.
$x = 5;
$x++; // 6
$x--; // 5| Operator | Meaning | Example | Notes |
|---|---|---|---|
++$x | Pre-increment | $y = ++$x; | $x is increased before it is used |
$x++ | Post-increment | $y = $x++; | $x is increased after it is used |
--$x | Pre-decrement | $y = --$x; | $x is decreased before it is used |
$x-- | Post-decrement | $y = $x--; | $x is decreased after it is used |
When multiple operators appear in one statement, PHP follows an order of evaluation — similar to math rules.
()$result = 5 + 3 * 2; // 11, not 16Because multiplication happens before addition.
Create a file math.php:
<?php
$a = 10;
$b = 5;
echo "Addition: " . ($a + $b) . "<br>";
echo "Subtraction: " . ($a - $b) . "<br>";
echo "Multiplication: " . ($a * $b) . "<br>";
echo "Division: " . ($a / $b) . "<br>";
echo "Remainder: " . ($a % $b) . "<br>";
?>✅ Output should show all five arithmetic results.
<?php
$x = 20;
$y = "20";
var_dump($x == $y);
var_dump($x === $y);
var_dump($x != $y);
var_dump($x !== $y);
?>✅ Observe which ones are true vs false — this demonstrates the difference between value and type.
<?php
$age = 25;
$has_license = true;
if ($age >= 18 && $has_license) {
echo "You are allowed to drive.";
} else {
echo "You are not allowed to drive.";
}
?>✅ Try changing $has_license to false and reloading the page.
<?php
$count = 1;
while ($count <= 5) {
echo "Count is: $count <br>";
$count++;
}
?>✅ Outputs 1 to 5. 💡 Replace ++ with += 2 to count by twos!
Goal: Build a script that calculates a final price after applying a percentage discount.
Create a file called discount.php:
<?php
$price = 100;
$discount = 15; // in percent
$final_price = $price - ($price * ($discount / 100));
echo "Original Price: $$price <br>";
echo "Discount: $discount% <br>";
echo "Final Price: $$final_price";
?>✅ Output:
Original Price: $100
Discount: 15%
Final Price: $85| # | Question | Options | Correct |
|---|---|---|---|
| 1 | What does the % operator do? | a) Divides numbers · b) Finds remainder · c) Multiplies numbers | b |
| 2 | Which operator checks value and type equality? | a) == · b) === · c) != | b |
| 3 | What is the result of 5 + 3 * 2? | a) 11 · b) 16 · c) 13 | a |
| 4 | What is the opposite of &&? | a) == · b) || · c) ! | b |
| 5 | What will $x++ do? | a) Increase before using · b) Increase after using · c) Do nothing | b |
Objective: Combine operators, comparisons, and logic to build a grade evaluator.
grade.php.$score = 87;<?php
$score = 87;
if ($score >= 90) {
$grade = "A";
} elseif ($score >= 80) {
$grade = "B";
} elseif ($score >= 70) {
$grade = "C";
} elseif ($score >= 60) {
$grade = "D";
} else {
$grade = "F";
}
echo "Score: $score<br>Grade: $grade";
?>✅ Test multiple values for $score and confirm the logic works.
math.php → arithmetic demodiscount.php → correct final price calculationgrade.php → grade output with logic
🏁 Next Step: In Module 4, you’ll take these skills and learn to control the flow of your programs —
using if statements, loops, and switch cases to make PHP truly interactive.