1. Conditional Statements
Conditionals allow your application to execute different code blocks based on logical decisions.
A. if...else and elseif
= 90) {
echo "Grade: A+ (Outstanding)";
} elseif ($marks >= 75) {
echo "Grade: A (Distinction)";
} elseif ($marks >= 50) {
echo "Grade: B (Pass)";
} else {
echo "Grade: F (Needs Improvement)";
}
B. switch Statement
Executes one code block from several choices when comparing a single expression:
2. Loops in PHP
Loops execute a block of statements repeatedly as long as a specified condition remains true.
A. while and do...while Loops
";
$num++;
}
// do-while always executes at least once
$count = 10;
do {
echo "Executed once even though condition is false: $count
";
$count++;
} while ($count < 5);
B. for Loop
";
}
C. foreach Loop (Arrays)
The foreach loop is specifically tailored for traversing arrays without needing manual counter tracking:
"New Delhi",
"Japan" => "Tokyo",
"France" => "Paris"
];
foreach ($capitals as $country => $city) {
echo "The capital of $country is $city.
";
}
3. Loop Control: break and continue
breakterminates loop execution immediately.continueskips the rest of the current iteration and advances to the next step.