MCQs on Control Structures | PHP Basics

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

1. Conditional Statements (if, else, else if, switch)

  1. Which PHP keyword is used to check a condition?
    • A) switch
    • B) if
    • C) while
    • D) foreach
  2. 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
  3. How do you write an else if statement in PHP?
    • A) else-if
    • B) elseif
    • C) else if
    • D) elif
  4. Which control structure is used to test multiple conditions?
    • A) for loop
    • B) if-else
    • C) switch
    • D) while
  5. 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
  6. 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
  7. 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)

  1. Which loop is best suited for iterating over an array?
    • A) for loop
    • B) while loop
    • C) do-while loop
    • D) foreach loop
  2. What will the following code print? for ($i = 0; $i < 3; $i++) { echo $i; }
    • A) 012
    • B) 123
    • C) 321
    • D) Error
  3. 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
  4. 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
  5. 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)
  6. 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
  7. 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
  8. How can you prematurely stop the execution of a loop?
    • A) exit
    • B) break
    • C) continue
    • D) return

3. break, continue, and exit

  1. 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
  2. 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
  3. 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
  4. Which statement can be used to stop script execution completely?
    • A) break
    • B) continue
    • C) exit
    • D) return
  5. Can exit accept a message as an argument?
    • A) Yes, in quotes
    • B) No
    • C) Only integers
    • D) None of the above
  6. 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
  7. 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
  8. 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
  9. 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
  10. Which statement is used to stop a foreach loop from iterating further?
    • A) return
    • B) break
    • C) continue
    • D) exit
  11. How can you avoid infinite loops?
    • A) Use continue wisely
    • B) Ensure proper loop conditions
    • C) Use break statements
    • D) All of the above
  12. Can you use exit within a loop?
    • A) Yes
    • B) No
    • C) Only with certain conditions
    • D) Only in nested loops
  13. 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
  14. 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
  15. Which command allows for conditional script termination?
    • A) continue
    • B) break
    • C) exit
    • D) stop

Answer Key

QnoAnswer (Option with the text)
1B) if
2A) Greater
3B) elseif
4C) switch
5A) Start of the week
6C) Both A and B
7C) case 10: continue;
8D) foreach loop
9A) 012
10A) 5 times
11C) do-while loop
12B) foreach ($array as $value)
13C) 13
14C) foreach loop
15B) break
16B) Exits the loop
17B) To skip the rest of the code in the current iteration
18C) 1
19C) exit
20A) Yes, in quotes
21A) 012345Stopped
22B) Prints “Hello” once
23B) To skip the rest of the current loop iteration
24A) Stops two nested loops
25B) break
26D) All of the above
27A) Yes
28B) 02
29D) Terminates script
30C) exit

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top