Control structures are fundamental in PHP programming, enabling developers to control the flow of execution in their scripts. They include conditional statements, loops, and branching mechanisms. This guide covers PHP control structures from beginner to advanced levels and includes multiple-choice questions (MCQs) to test your knowledge.
Join to Become PHP Web Developer - Future Scope Career Guidance
Introduction to PHP Control Structures
Control structures in PHP determine the execution flow of the script. They can be categorized into:
- Conditional Statements: if, if-else, switch
- Looping Structures: for, while, do-while, foreach
- Branching Statements: break, continue, return, exit
Conditional Statements
if Statement
The if statement executes a block of code only if a specified condition is true.
$age = 22;
if ($age >= 18) {
echo "You are eligible to vote.";
}In the above code, “You are eligible to vote.” will print, because $age >= 18 condition is true. The value of $age is 22, which means 22 >= 18, 22 is greater than 18, so the result will print.
if-else Statement
If the condition is false, an alternative block of code executes.
$score = 50;
if ($score >= 60) {
echo "Pass";
} else {
echo "Fail";
}In the above code, “Fail” will print, because $score >= 60 condition is false. The value of $score is 50, which means 50 >= 60, 50 is not greater than 60, so the result “Fail” will print.
if-elseif-else Statement
Used to check multiple conditions sequentially.
$marks = 85;
if ($marks > 90)
{
echo "Grade A";
}
elseif ($marks > 75)
{
echo "Grade B";
}
else
{
echo "Grade C";
}In the above code, “Grade B” will print, because the value of $marks is 85, in if condition we are checking that is 85 >= 90 (is 85 greater than 90) result is no, the condition is not true, so the “Grade A” will not print.
Now in the else if condition checking that is 85 > 75 (is 85 greater than 90), the condition is true, so the “Grade B ” will print.
if-elseif-else Statement
The switch statement evaluates an expression and executes matching cases.
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the workweek!";
break;
case "Friday":
echo "Weekend is near!";
break;
default:
echo "Regular day.";
}In the above code, “Start of the workweek!” will print. Because the value of $day is Monday. In the case “Monday” we are checking that is value Monday, here condition is matched, so the result is printed.
Looping Structures
for Loop
Executes a block of code a specified number of times.
for ($i = 1; $i <= 5; $i++) {
echo "Iteration $i <br>";
}while Loop
Executes as long as a condition remains true.
$x = 1;
while ($x <= 5) {
echo "Number: $x <br>";
$x++;
}do while Loop
Executes at least once, then repeats as long as the condition is true.
$y = 1;
do {
echo "Number: $y <br>";
$y++;
} while ($y <= 5);foreach Loop
Used for iterating over arrays.
$colors = ["Red", "Green", "Blue"];
foreach ($colors as $color) {
echo "Color: $color <br>";
}Multiple-Choice Questions (MCQs)
Which loop is best when the number of iterations is known?
A) while
B) do-while
C) for
D) foreach
Answer: C) for
What is the output of the following code?
for ($i = 0; $i < 3; $i++) {
echo $i;
}A) 012
B) 123
C) 345
D) 678
Answer: A) 012
Which statement is used to exit a loop?
A) break
B) continue
C) exit
D) return
Answer: A) break
What is the output of the following code?
$x = 0;
do {
echo $x;
$x++;
} while ($x < 0);A) 0
B) No output
C) Infinite loop
D) 012345
Answer: A) 0



