PHP: For Loops

Example 1

1: Hi my name is Sir Chris

2: Hi my name is Sir Chris

3: Hi my name is Sir Chris

4: Hi my name is Sir Chris


Example 2

  1. Chocolate Chip Cookies.
  2. Oatmeal Raison Cookies.
  3. Snickerdoodle Cookies.
  4. Grasshopper Cookies.

Example 3

Chris Tow is a 38 year old gem of a client.

Rick Jebley is a 65 year old piece of shit.

Ronald O'Neal is a 33 year old piece of shit.

Inga Marie is a 40 year old gem of a client.


The code used for index.php is below:


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="../styles.css">
    <title>PHP: For Loops</title>
</head>
<body>

<h1 class="lesson-h1">PHP: For Loops</h1>
    
<?php

// Example 1 Code /////////////////////////////////////////////
$num = 0;
echo "<h4>Example 1</h4>";
for($i = 0; $i < 4; $i++){
    echo "<p>".($num + 1).": Hi my name is Sir Chris</p>";
    $num++;
}

// Example 2 Code /////////////////////////////////////////////
echo "<hr><h4>Example 2</h4>";

$cookies = [
    "Chocolate Chip",
    "Oatmeal Raison",
    "Snickerdoodle",
    "Grasshopper"
];

echo "<ol>";
for ($i = 0; $i < count($cookies); $i++) {
    echo "<li>$cookies[$i] Cookies.</li>";
}
echo "</ol>";

// Example 3 Code /////////////////////////////////////////////
echo "<hr><h4>Example 3</h4>";

$clients = [
    ["name" => "Chris Tow", "age" => 38, "is_cool" => true],
    ["name" => "Rick Jebley", "age" => 65, "is_cool" => false],
    ["name" => "Ronald O'Neal", "age" => 33, "is_cool" => false],
    ["name" => "Inga Marie", "age" => 40, "is_cool" => true],
];

for ($i = 0; $i < count($clients); $i++) {

    if($clients[$i]["is_cool"] === false){
        echo "<h3>" . $clients[$i]['name'] . " is a " . $clients[$i]['age'] . " year old piece of shit.</h3>";
    } else {
        echo "<h3>" . $clients[$i]['name'] . " is a " . $clients[$i]['age'] . " year old gem of a client.</h3>";
    }
}

include('../show_code.php');
show_code('index.php');
?>

</body>
</html>