Control structures in PHP are crucial for efficient programming. They include conditional statements, loops, and flow-control commands like break, continue, and exit. This guide covers everything you need to confidently write robust PHP code.
PHP Control Structures – 30 Multiple Choice Questions
What will be the output of the following code? $x = 10; if ($x > 5) { echo "Greater"; } else { echo "Lesser"; }
A) Greater
B) Lesser
C) 10
D) Error
How do you write an else if statement in PHP?
A) else-if
B) elseif
C) else if
D) elif
Which control structure is used to test multiple conditions?
A) for loop
B) if-else
C) switch
D) while
What is the output of the following switch statement? $day = "Monday"; switch ($day) { case "Monday": echo "Start of the week"; break; default: echo "Invalid day"; }
A) Start of the week
B) Invalid day
C) Monday
D) Error
How can you combine multiple conditions in an if statement?
A) Using && and ||
B) Using and and or
C) Both A and B
D) None of the above
Which of the following is not a valid switch case format?
A) case “value”: break;
B) case 10: break;
C) case 10: continue;
D) case 10: exit;
2. Loops (for, while, do-while, foreach)
Which loop is best suited for iterating over an array?
A) for loop
B) while loop
C) do-while loop
D) foreach loop
What will the following code print? for ($i = 0; $i < 3; $i++) { echo $i; }
A) 012
B) 123
C) 321
D) Error
How many times will the loop execute? $i = 0; while ($i < 5) { echo $i++; }
A) 5 times
B) 6 times
C) Infinite loop
D) 4 times
Which loop executes at least once even if the condition is false?
A) for loop
B) while loop
C) do-while loop
D) foreach loop
What is the correct syntax for a foreach loop?
A) foreach ($array : $value)
B) foreach ($array as $value)
C) foreach ($array => $value)
D) foreach ($array; $value)
What will the following code output? $arr = [1, 2, 3]; foreach ($arr as $val) { if ($val == 2) continue; echo $val; }
A) 123
B) 12
C) 13
D) 23
Which loop can be used to iterate over associative arrays in PHP?
A) for loop
B) while loop
C) foreach loop
D) do-while loop
How can you prematurely stop the execution of a loop?
A) exit
B) break
C) continue
D) return
3. break, continue, and exit
What does the break statement do in a loop?
A) Skips the current iteration
B) Exits the loop
C) Continues the next iteration
D) Stops script execution
In which scenarios is the continue statement useful?
A) To exit a loop entirely
B) To skip the rest of the code in the current iteration
C) To pause loop execution
D) To exit the script
What will the output be for the following code? for ($i = 1; $i <= 3; $i++) { if ($i == 2) break; echo $i; }
A) 123
B) 12
C) 1
D) None
Which statement can be used to stop script execution completely?
A) break
B) continue
C) exit
D) return
Can exit accept a message as an argument?
A) Yes, in quotes
B) No
C) Only integers
D) None of the above
What will the output be? $i = 0; do { if ($i > 5) exit("Stopped"); echo $i; $i++; } while ($i < 10);
A) 012345Stopped
B) 01234
C) Infinite loop
D) Error
What does the following code do? while (true) { echo "Hello"; break; }
A) Prints “Hello” infinitely
B) Prints “Hello” once
C) Causes an error
D) Prints nothing
When should you use continue instead of break?
A) To stop the loop entirely
B) To skip the rest of the current loop iteration
C) To terminate script execution
D) To restart the loop
What does the break 2 statement mean?
A) Stops two nested loops
B) Skips two iterations
C) Breaks out of two switch cases
D) Exits the script with error code 2
Which statement is used to stop a foreach loop from iterating further?
A) return
B) break
C) continue
D) exit
How can you avoid infinite loops?
A) Use continue wisely
B) Ensure proper loop conditions
C) Use break statements
D) All of the above
Can you use exit within a loop?
A) Yes
B) No
C) Only with certain conditions
D) Only in nested loops
What is the result of the following code? for ($i = 0; $i < 3; $i++) { if ($i == 1) continue; echo $i; }
A) 012
B) 02
C) 1
D) None
What will be the effect of the following code?phpCopy codeexit(0); echo "Hello";
A) Prints “Hello”
B) Prints 0
C) Does nothing
D) Terminates script
Which command allows for conditional script termination?
A) continue
B) break
C) exit
D) stop
Answer Key
Qno
Answer (Option with the text)
1
B) if
2
A) Greater
3
B) elseif
4
C) switch
5
A) Start of the week
6
C) Both A and B
7
C) case 10: continue;
8
D) foreach loop
9
A) 012
10
A) 5 times
11
C) do-while loop
12
B) foreach ($array as $value)
13
C) 13
14
C) foreach loop
15
B) break
16
B) Exits the loop
17
B) To skip the rest of the code in the current iteration