Chapter 6: Conditional Statements in PHP — Online MCQ Test
COMPUTER APPLICATIONS · CLASS 12th · Tamil Nadu State Board
Practice Chapter 6: Conditional Statements in PHP with a free chapter-wise online MCQ test.
This chapter covers: This chapter covers logical decision-making in PHP web development. Students examine control structures including if statements if-else conditions if-elseif-else chains and switch....
AI-generated questions from basic to board-exam level, with instant results and explanations.
Chapter 6: Conditional Statements in PHP — Important Questions & Answers
Which PHP statement is used to execute a block of code only when a condition is true?
- A. if
- B. switch
- C. for
- D. while
Answer: A. if
The if statement checks a condition and runs the code block only when the condition evaluates to true.
The if statement checks a condition and runs the code block only when the condition evaluates to true.
What is the correct syntax of an if statement in PHP?
- A. if (condition) { statements; }
- B. if condition then { statements; }
- C. if { condition } (statements);
- D. if [condition] do statements;
Answer: A. if (condition) { statements; }
In PHP, the condition is placed inside parentheses and the statements are enclosed in curly braces.
In PHP, the condition is placed inside parentheses and the statements are enclosed in curly braces.
What will be the output of the following code? <?php $x = 10; if ($x > 5) { echo 'Yes'; } ?>
- A. Yes
- B. No
- C. 10
- D. Error
Answer: A. Yes
Since 10 is greater than 5, the condition is true and 'Yes' is displayed.
Since 10 is greater than 5, the condition is true and 'Yes' is displayed.
Choose the correct output: <?php $mark = 45; if ($mark >= 90) echo 'A'; elseif ($mark >= 50) echo 'B'; else echo 'C'; ?>
- A. B
- B. A
- C. C
- D. 45
Answer: C. C
45 is neither 90 or above nor 50 or above, so the else part executes and prints 'C'.
45 is neither 90 or above nor 50 or above, so the else part executes and prints 'C'.
Which of the following switch statements is syntactically correct in PHP?
- A. switch($day) { case 1: echo 'Mon'; break; default: echo 'Invalid'; }
- B. switch $day { case 1 echo 'Mon'; break; }
- C. switch($day) [ case 1 => echo 'Mon'; ]
- D. switch($day) { case 1; echo 'Mon'; break }
Answer: A. switch($day) { case 1: echo 'Mon'; break; default: echo 'Invalid'; }
PHP switch syntax requires case labels with colons, and statements inside braces, with break commonly used after each case.
PHP switch syntax requires case labels with colons, and statements inside braces, with break commonly used after each case.