📅 Week 9 - Day 2: Arrays, Conditionals & Loops

Wednesday | Duration: 3 hours

🎯 Learning Objectives

📦 PHP Arrays

Arrays are variables that can store multiple values. PHP supports indexed arrays (with numeric keys) and associative arrays (with named keys).

1. Indexed Arrays

<?php // Creating indexed array $fruits = array("Apple", "Banana", "Orange"); // Or using short syntax $colors = ["Red", "Green", "Blue"]; // Accessing array elements (index starts at 0) echo $fruits[0]; // Apple echo $fruits[1]; // Banana echo $fruits[2]; // Orange // Array length echo count($fruits); // 3 // Adding elements $fruits[] = "Mango"; // Add to end $colors[3] = "Yellow"; // Add at specific index ?>

2. Associative Arrays

<?php // Array with named keys $student = array( "name" => "Ahmad Garba", "age" => 25, "city" => "Kano", "course" => "Web Development" ); // Or using short syntax $product = [ "name" => "Laptop", "price" => 50000, "brand" => "HP" ]; // Accessing values echo $student["name"]; // Ahmad Garba echo $product["price"]; // 50000 // Adding new key-value pair $student["email"] = "ahmad@example.com"; ?>

3. Multidimensional Arrays

<?php $students = [ ["name" => "Ahmad", "age" => 25], ["name" => "Fatima", "age" => 23], ["name" => "Ibrahim", "age" => 27] ]; // Access nested arrays echo $students[0]["name"]; // Ahmad echo $students[1]["age"]; // 23 ?>

🔀 Conditional Statements

if Statement

<?php $age = 20; if ($age >= 18) { echo "You are an adult"; } ?>

if...else Statement

<?php $score = 75; if ($score >= 50) { echo "You passed!"; } else { echo "You failed."; } ?>

if...elseif...else Statement

<?php $grade = 85; if ($grade >= 90) { echo "Grade: A"; } elseif ($grade >= 80) { echo "Grade: B"; } elseif ($grade >= 70) { echo "Grade: C"; } elseif ($grade >= 60) { echo "Grade: D"; } else { echo "Grade: F"; } ?>

Comparison Operators

Operator Meaning Example
==Equal to$a == $b
===Identical (value & type)$a === $b
!=Not equal$a != $b
<Less than$a < $b
>Greater than$a > $b
<=Less than or equal$a <= $b
>=Greater than or equal$a >= $b

Logical Operators

<?php $age = 25; $hasLicense = true; // AND operator if ($age >= 18 && $hasLicense) { echo "You can drive"; } // OR operator if ($age < 18 || !$hasLicense) { echo "You cannot drive"; } // NOT operator if (!$hasLicense) { echo "No license"; } ?>

🔁 Loops

1. for Loop

<?php // Print numbers 1 to 10 for ($i = 1; $i <= 10; $i++) { echo $i . " "; } // Loop through indexed array $fruits = ["Apple", "Banana", "Orange", "Mango"]; for ($i = 0; $i < count($fruits); $i++) { echo $fruits[$i] . "<br>"; } ?>

2. while Loop

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

3. foreach Loop (for Arrays)

<?php // Loop through indexed array $colors = ["Red", "Green", "Blue"]; foreach ($colors as $color) { echo $color . "<br>"; } // Loop through associative array $student = [ "name" => "Ahmad", "age" => 25, "city" => "Kano" ]; foreach ($student as $key => $value) { echo $key . ": " . $value . "<br>"; } ?>

💻 Complete Example: Student Grade System

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Student Grade System</title> <style> body { font-family: Arial, sans-serif; max-width: 900px; margin: 50px auto; padding: 20px; background: #f5f5f5; } table { width: 100%; background: white; border-collapse: collapse; box-shadow: 0 5px 15px rgba(0,0,0,0.1); } th, td { padding: 15px; text-align: left; border-bottom: 1px solid #ddd; } th { background: #ff6b6b; color: white; } .pass { color: green; font-weight: bold; } .fail { color: red; font-weight: bold; } </style> </head> <body> <h1>Student Grade Report</h1> <?php // Array of students with scores $students = [ ["name" => "Ahmad Garba", "score" => 85], ["name" => "Fatima Yusuf", "score" => 92], ["name" => "Ibrahim Musa", "score" => 67], ["name" => "Aisha Mohammed", "score" => 78], ["name" => "Usman Ali", "score" => 45] ]; echo "<table>"; echo "<tr>"; echo "<th>Student Name</th>"; echo "<th>Score</th>"; echo "<th>Grade</th>"; echo "<th>Status</th>"; echo "</tr>"; foreach ($students as $student) { $name = $student["name"]; $score = $student["score"]; // Determine grade if ($score >= 90) { $grade = "A"; } elseif ($score >= 80) { $grade = "B"; } elseif ($score >= 70) { $grade = "C"; } elseif ($score >= 60) { $grade = "D"; } else { $grade = "F"; } // Determine pass/fail if ($score >= 50) { $status = "PASS"; $statusClass = "pass"; } else { $status = "FAIL"; $statusClass = "fail"; } echo "<tr>"; echo "<td>" . $name . "</td>"; echo "<td>" . $score . "</td>"; echo "<td>" . $grade . "</td>"; echo "<td class='" . $statusClass . "'>" . $status . "</td>"; echo "</tr>"; } echo "</table>"; // Calculate statistics $totalScore = 0; $passCount = 0; foreach ($students as $student) { $totalScore += $student["score"]; if ($student["score"] >= 50) { $passCount++; } } $average = $totalScore / count($students); $passPercentage = ($passCount / count($students)) * 100; echo "<h2>Statistics</h2>"; echo "<p>Total Students: " . count($students) . "</p>"; echo "<p>Average Score: " . round($average, 2) . "</p>"; echo "<p>Students Passed: " . $passCount . "</p>"; echo "<p>Pass Rate: " . round($passPercentage, 2) . "%</p>"; ?> </body> </html>

✏️ Practice Exercises

  1. Create an array of 5 favorite foods and display them in an HTML list
  2. Build an associative array of 3 products with name and price, display in table
  3. Write a script that checks if a number is even or odd
  4. Use a for loop to print multiplication table (1-10)
  5. Create an array of temperatures and find the highest using a loop

✅ Today's Deliverable

← Previous Course Home Next: Day 3 →