Chapter 3: Operators
Learning Objectives
By the end of this chapter, you will be able to:
- Perform basic mathematical calculations using arithmetic operators.
- Understand and use assignment operators to modify the values of variables.
- Compare two values using comparison operators to get a Boolean (
true
/false
) result. - Combine multiple conditions using logical operators.
Introduction: The Chef's Tools
In the previous chapter, we prepared our "ingredients" (variables and data types). In this chapter, we'll learn about the "kitchen tools" that our chef (PHP) uses to cook those ingredients. Whether it's a knife, a spoon, or a stove, these tools in the programming world are called Operators.
Operators are special symbols that perform specific operations on data, such as adding numbers, comparing values, or assigning a new value to a variable.
3.1 Arithmetic Operators
This is the most straightforward group, used for performing mathematical calculations.
+
(Addition)-
(Subtraction)*
(Multiplication)/
(Division)%
(Modulo) - This gives you the remainder of a division. It's very useful for tasks like checking if a number is even or odd.
Example:
<?php
$price = 100;
$quantity = 3;
$total = $price * $quantity; // 100 * 3
$score = 10;
$pointsAdded = 5;
$newScore = $score + $pointsAdded; // 10 + 5
$items = 10;
$itemsPerBox = 3;
$itemsLeftOver = $items % $itemsPerBox; // 10 divided by 3 is 3 with a remainder of 1
echo "<p>Total Price: " . $total . "</p>"; // Outputs: Total Price: 300
echo "<p>New Score: " . $newScore . "</p>"; // Outputs: New Score: 15
echo "<p>Items Left Over: " . $itemsLeftOver . "</p>"; // Outputs: Items Left Over: 1
?>
3.2 Assignment Operators
These are used to assign values to variables. You already know the basic one, =
, but there are also shorthand versions that make your code cleaner.
=
: Assign a value.+=
: Add a value to the variable.-=
: Subtract a value from the variable..=
: Append text to a string variable.
Example:
<?php
$score = 100;
// The long way: $score = $score + 5;
$score += 5; // The shorthand way
echo "<p>Score: " . $score . "</p>"; // Outputs: Score: 105
$greeting = "Hello";
// The long way: $greeting = $greeting . " World!";
$greeting .= " World!"; // The shorthand way for strings
echo "<p>" . $greeting . "</p>"; // Outputs: Hello World!
?>
3.3 Comparison Operators
These are used to compare two values. The result is always a Boolean (true
or false
), which is essential for making decisions in your code.
==
: Equal (checks if the values are the same, ignores data type)===
: Identical (checks if the values AND the data types are the same)!=
: Not equal>
: Greater than<
: Less than>=
: Greater than or equal to<=
: Less than or equal to
The Critical Difference: == vs. ===
This is a common point of confusion for new programmers but is a vital concept for writing accurate code.
<?php
$a = 5; // This is an Integer
$b = "5"; // This is a String
var_dump($a == $b); // Outputs: bool(true) because the "value" is the same.
var_dump($a === $b); // Outputs: bool(false) because the "data type" is different (Integer vs. String).
?>
Academic Recommendation: To prevent unexpected bugs and ensure strict accuracy, you should almost always use the identical operator (===
) for comparisons.
3.4 Logical Operators
These are used to combine multiple conditional statements.
&&
(AND): Istrue
only if all conditions are true.||
(OR) : Istrue
if at least one condition is true.!
(NOT): Inverts the value (turnstrue
tofalse
, andfalse
totrue
).
Example:
<?php
$age = 25;
$isMember = true;
// AND: Must be older than 18 AND a member
if ($age > 18 && $isMember == true) {
echo "<p>Welcome, valued member!</p>";
}
// OR: If today is Saturday OR Sunday
$today = "Sunday";
if ($today == "Saturday" || $today == "Sunday") {
echo "<p>Happy Weekend!</p>";
}
// NOT: If NOT logged in
$isLoggedIn = false;
if (!$isLoggedIn) { // !false becomes true
echo "<p>Please log in.</p>";
}
?>
Conclusion & What's Next
In this chapter, you learned how to use the "tools" (Operators) to work with your "ingredients" (Variables). You can now perform calculations, assign new values, and compare data to create logic in your programs.
Now that we can get a true
or false
result from comparisons, the next step is to tell our program how to make decisions and take different actions based on those results. In the next chapter, we will master "Chapter 4: Control Structures," where we will dive into if-else
statements and loops.