Chapter 6: Control Structures
Chapter 6 of 15
Chapter 6: Control Structures
6.1 Conditional Statements
Control program flow based on conditions.
if ($age >= 18) {
echo "Adult";
} elseif ($age >= 13) {
echo "Teenager";
} else {
echo "Child";
}
6.2 Switch Statements
switch ($day) {
case "Monday":
echo "Start of week";
break;
case "Friday":
echo "End of week";
break;
default:
echo "Midweek";
}
6.3 Loops
Repeat code execution.
// For loop
for ($i = 0; $i < 10; $i++) {
echo $i;
}
// While loop
while ($i < 10) {
echo $i;
$i++;
}
// Foreach loop (for arrays)
foreach ($array as $value) {
echo $value;
}