StreetGeek Academy · PHP Foundations

🧩 Module 3: Operators and Expressions

Objective: Understand how PHP performs mathematical calculations, logical comparisons,
and combines conditions to control program behavior.

🔹 1. What Are Operators?

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:

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators

🔹 2. Arithmetic Operators

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; // 1

🔹 3. Assignment Operators

These 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 5

🔹 4. Comparison Operators

Used 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)

🔹 5. Logical Operators

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.

🔹 6. Increment & Decrement

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

🧩 7. Operator Precedence

When multiple operators appear in one statement, PHP follows an order of evaluation — similar to math rules.

  • Parentheses ()
  • Multiplication / Division / Modulus
  • Addition / Subtraction
  • Comparison
  • Logical
$result = 5 + 3 * 2; // 11, not 16

Because multiplication happens before addition.

🧪 Hands-On Practice

Exercise 1: Simple Math

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.

Exercise 2: Comparison Practice

<?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.

Exercise 3: Logical Operator Check

<?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.

Exercise 4: Increment Challenge

<?php
$count = 1;
while ($count <= 5) {
    echo "Count is: $count <br>";
    $count++;
}
?>

✅ Outputs 1 to 5. 💡 Replace ++ with += 2 to count by twos!

🧮 Mini Project – Discount Calculator

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

🧾 Module 3 Quiz

# 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

💪 Challenge Task – Simple Grade Calculator

Objective: Combine operators, comparisons, and logic to build a grade evaluator.

  1. Create a file named grade.php.
  2. Ask for a student’s score (assign manually for now):
$score = 87;
  1. Use conditional statements to determine the letter grade:
<?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.

🧾 Submission Checklist

  • math.php → arithmetic demo
  • discount.php → correct final price calculation
  • grade.php → grade output with logic
  • ✅ Quiz answers filled out

🏁 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.