🚀 PHP Control Structures: Mastering if-else, switch, loops & More!
📌 What Are Control Structures in PHP?
Control structures control the flow of your PHP program.
✅ Main Types of Control Structures: 1️⃣ Conditional Statements – if, else, elseif, switch 2️⃣ Looping Structures – for, while, do-while, foreach 3️⃣ Jump Statements – break, continue, exit, die
1️⃣ Conditional Statements: Making Decisions
✅ if Statement
Executes a block of code if the condition is true.
<?php
$age = 20;
if ($age >= 18) {
echo "You are an adult."; // ✅ Outputs: You are an adult.
}
?>
✅ if-else Statement
Executes one block if true, another block if false.
<?php
$age = 16;
if ($age >= 18) {
echo "You can vote.";
} else {
echo "You are too young to vote."; // ✅ Outputs: You are too young to vote.
}
?>
✅ if-elseif-else Statement
Used when you have multiple conditions.
<?php
$marks = 85;
if ($marks >= 90) {
echo "Grade: A+";
} elseif ($marks >= 80) {
echo "Grade: A"; // ✅ Outputs: Grade: A
} elseif ($marks >= 70) {
echo "Grade: B";
} else {
echo "Grade: C";
}
?>
✅ switch Statement
Best when checking multiple values of a single variable.
<?php
$day = "Tuesday";
switch ($day) {
case "Monday":
echo "Start of the week!";
break;
case "Tuesday":
echo "Keep going!"; // ✅ Outputs: Keep going!
break;
case "Friday":
echo "Weekend is near!";
break;
default:
echo "Just another day!";
}
?>
📌 When to Use if-elseif vs switch? ✔ Use if-elseif for complex conditions (e.g., if ($x > 10 && $y < 5)) ✔ Use switch when checking specific values (e.g., switch ($day))
2️⃣ Looping Structures: Repeating Code Efficiently
Loops execute a block of code multiple times.
✅ for Loop (Fixed Repetitions)
Runs a set number of times.
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Number: $i <br>";
}
/*
✅ Outputs:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
*/
?>
📌 Best for: When you know how many times the loop should run.
✅ while Loop (Runs Until Condition is False)
Executes as long as the condition is true.
<?php
$x = 1;
while ($x <= 5) {
echo "Value: $x <br>";
Recommended by LinkedIn
$x++;
}
/*
✅ Outputs:
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5
*/
?>
📌 Best for: When the number of repetitions isn’t known in advance.
✅ do-while Loop (Always Runs At Least Once)
Runs at least once, even if the condition is false.
<?php
$y = 6;
do {
echo "Number: $y <br>"; // ✅ Runs once, even if $y > 5
$y++;
} while ($y <= 5);
?>
📌 Best for: When you need to execute the loop at least once.
✅ foreach Loop (For Arrays)
Used only for arrays.
<?php
$colors = ["Red", "Blue", "Green"];
foreach ($colors as $color) {
echo "Color: $color <br>";
}
/*
✅ Outputs:
Color: Red
Color: Blue
Color: Green
*/
?>
📌 Best for: Looping through arrays & objects.
3️⃣ Jump Statements: Controlling Loop Execution
Jump statements modify the loop execution.
✅ break (Stops the Loop)
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) break; // Stops at 3
echo "Number: $i <br>";
}
/*
✅ Outputs:
Number: 1
Number: 2
*/
?>
✅ continue (Skips the Current Iteration)
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) continue; // Skips 3
echo "Number: $i <br>";
}
/*
✅ Outputs:
Number: 1
Number: 2
Number: 4
Number: 5
*/
?>
✅ exit() and die() (Stop the Entire Script)
<?php
echo "Hello World!";
exit(); // ✅ Stops execution here
echo "This will not be printed!";
?>
🔹 exit() and die() are identical.
🔹 Summary: PHP Control Structures (Basic to Advanced)
✔ Conditional Statements – if, else, elseif, switch ✔ Looping Structures – for, while, do-while, foreach ✔ Jump Statements – break, continue, exit, die
🔥 What’s Next?
👉 PHP Functions – Creating Reusable Code!
.
.
#PHP #WebDevelopment #Coding #Programming #PHPTutorial #SoftwareDevelopment #Tech #LearnToCode #BackendDevelopment #PHPDevelopers #WebDev #CodingTips #Looping #ConditionalStatements #ControlStructures #PHPProgramming