Objective:
The goal of this lesson is to familiarize learners with the usage of operators in PHP. By the end of this lesson, participants should be able to:
- Understand and apply arithmetic operators for mathematical operations.
- Use comparison operators to compare values and make decisions.
- Employ logical operators to perform logical operations.
- Combine different operators effectively in expressions.
Description:
2.1 Arithmetic Operators
2.1.1 Addition +
Learn how to add two values together using the addition operator.
2.1.2 Subtraction -
Understand subtraction and how to find the difference between two values.
2.1.3 Multiplication *
Explore multiplication and how to find the product of two values.
2.1.4 Division /
Learn about division and how to find the quotient of two values.
2.1.5 Modulus %
Introduce the modulus operator for finding the remainder of a division.
<?php
$num1 = 10;
$num2 = 3;
echo $num1 + $num2; // Outputs: 13
echo $num1 - $num2; // Outputs: 7
echo $num1 * $num2; // Outputs: 30
echo $num1 / $num2; // Outputs: 3.3333...
echo $num1 % $num2; // Outputs: 1
?>
2.2 Comparison Operators
2.2.1 Equal ==
Learn how to check if two values are equal.
2.2.2 Not Equal !=
Understand the not equal operator for checking inequality.
2.2.3 Greater Than >
Explore the greater than operator for comparisons.
2.2.4 Less Than <
Introduce the less than operator for comparisons.
2.2.5 Greater Than or Equal To >=
and Less Than or Equal To <=
Learn how to check for greater than or equal to and less than or equal to.
<?php
$num1 = 10;
$num2 = 5;
echo $num1 == $num2; // Outputs: false
echo $num1 != $num2; // Outputs: true
echo $num1 > $num2; // Outputs: true
echo $num1 < $num2; // Outputs: false
echo $num1 >= $num2; // Outputs: true
echo $num1 <= $num2; // Outputs: false
?>
2.3 Logical Operators
2.3.1 AND &&
Understand the AND operator for combining conditions.
2.3.2 OR ||
Learn how to use the OR operator for combining conditions.
2.3.3 NOT !
Introduce the NOT operator for negating a condition.
<?php
$isSunny = true;
$isWarm = false;
echo ($isSunny && $isWarm); // Outputs: false
echo ($isSunny || $isWarm); // Outputs: true
echo !$isSunny; // Outputs: false
?>
2.4 Combining Operators in Expressions
Combine different operators in expressions to create more complex conditions and computations.
<?php
$num1 = 15;
$num2 = 7;
$num3 = 3;
$result = ($num1 - $num2) * $num3 / 2;
echo $result; // Outputs: 27
?>
2.5 Practice Exercise
Problem Statement:
You are developing a simple calculator program in PHP. The program should take two numbers and perform the following operations:
- Add the two numbers.
- Subtract the second number from the first.
- Multiply the two numbers.
- Divide the first number by the second (if the second number is not zero).
- Find the remainder when dividing the first number by the second (if the second number is not zero).
- Check if the two numbers are equal.
- Check if the first number is greater than the second.
<!DOCTYPE html>
<html>
<head>
<title>Simple Calculator</title>
</head>
<body>
<!-- Create a simple form for user input -->
<form method="POST" action="">
<label for="num1">Enter the first number:</label>
<input type="number" name="num1" required>
<label for="num2">Enter the second number:</label>
<input type="number" name="num2" required>
<input type="submit" value="Calculate">
</form>
<?php
// PHP code for handling form submission and performing calculations
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get user input
$num1 = $_POST["num1"];
$num2 = $_POST["num2"];
// Perform operations and display results
echo "<h2>Results:</h2>";
echo "1. Addition: $num1 + $num2 = " . ($num1 + $num2) . "<br>";
echo "2. Subtraction: $num1 - $num2 = " . ($num1 - $num2) . "<br>";
echo "3. Multiplication: $num1 * $num2 = " . ($num1 * $num2) . "<br>";
if ($num2 != 0) {
echo "4. Division: $num1 / $num2 = " . ($num1 / $num2) . "<br>";
echo "5. Modulus: $num1 % $num2 = " . ($num1 % $num2) . "<br>";
} else {
echo "4. Division: Cannot divide by zero<br>";
echo "5. Modulus: Cannot calculate modulus with zero<br>";
}
echo "6. Equality Check: $num1 == $num2 is " . ($num1 == $num2 ? 'true' : 'false') . "<br>";
echo "7. Comparison: $num1 > $num2 is " . ($num1 > $num2 ? 'true' : 'false') . "<br>";
}
?>
</body>
</html>
This code performs the specified operations based on user input and displays the results on the webpage. It also includes error handling for division and modulus operations when the second number is zero. Copy and paste this code into a PHP file, run it, and test the calculator with various input values.