Objective:
The aim of this lesson is to introduce learners to fundamental control structures in PHP, including conditional statements (if, else, elseif) and looping constructs (for, while, do-while). By the end of this lesson, participants should be able to:
- Understand the use of conditional statements for decision-making in code.
- Utilize if statements to execute code conditionally.
- Implement else and elseif statements for handling multiple conditions.
- Explore looping constructs to repeat code execution.
Description:
3.1 Conditional Statements (if, else, elseif)
3.1.1 Introduction to Conditional Statements
Understand the purpose of conditional statements in programming and their role in controlling the flow of code execution.
3.1.2 if Statement
Learn how to use the if
statement to execute code when a specified condition is true.
<?php
$age = 20;
if ($age >= 18) {
echo "You are an adult.";
}
?>
3.1.3 else Statement
Introduce the else
statement to execute code when the condition of the if
statement is false.
<?php
$age = 15;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
?>
3.1.4 elseif Statement
Explore the elseif
statement for handling multiple conditions in a structured manner.
<?php
$grade = 75;
if ($grade >= 90) {
echo "Excellent!";
} elseif ($grade >= 70) {
echo "Good job!";
} else {
echo "Keep working hard.";
}
?>
3.2 Looping Constructs (for, while, do-while)
3.2.1 Introduction to Looping Constructs
Understand the concept of loops and their role in executing code repeatedly.
3.2.2 for Loop
Learn how to use the for
loop for executing a block of code a specified number of times.
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Iteration $i<br>";
}
?>
3.2.3 while Loop
Explore the while
loop for executing code as long as a specified condition is true.
<?php
$counter = 1;
while ($counter <= 5) {
echo "Iteration $counter<br>";
$counter++;
}
?>
3.2.4 do-while Loop
Introduce the do-while
loop for executing code at least once, then repeating as long as a specified condition is true.
<?php
$counter = 1;
do {
echo "Iteration $counter<br>";
$counter++;
} while ($counter <= 5);
?>
3.3 Practice Exercise
Let’s create a simple practice exercise where we use conditional statements and a loop to generate a multiplication table for a given number. Copy and paste the following code into a PHP file:
<!DOCTYPE html>
<html>
<head>
<title>Multiplication Table Generator</title>
</head>
<body>
<!-- Create a simple form for user input -->
<form method="POST" action="">
<label for="number">Enter a number:</label>
<input type="number" name="number" required>
<input type="submit" value="Generate Table">
</form>
<?php
// PHP code for handling form submission and generating multiplication table
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get user input
$number = $_POST["number"];
// Validate input
if (!is_numeric($number) || $number <= 0) {
echo "Please enter a positive number.";
} else {
// Generate and display the multiplication table
echo "<h2>Multiplication Table for $number:</h2>";
echo "<table border='1'>";
for ($i = 1; $i <= 10; $i++) {
echo "<tr>";
echo "<td>$number x $i</td>";
echo "<td>=</td>";
echo "<td>" . ($number * $i) . "</td>";
echo "</tr>";
}
echo "</table>";
}
}
?>
</body>
</html>
In this exercise:
- The user is prompted to enter a number in a form.
- Upon submitting the form, the PHP code validates the input and generates a multiplication table for the entered number using a
for
loop. - The result is displayed in an HTML table.