TN Online TestSamacheer Kalvi practice

Conditional Statements in PHP: One Mark Questions, Book Back Answers and Notes

12th Standard · Computer Applications · #6

This chapter covers the four decision-making statements in PHP: if, if-else, if-elseif-else and switch. For each one you learn the syntax, the order in which PHP checks the conditions, and how to work out which block runs for a given value of a variable.

Study this chapter

Asked in previous board exams

6 questions from this chapter came in 6 past public exams. Open any of them to see the answer and explanation.

  1. March 2026
    Which of the following is used to terminate the switch statement?
    See answer in the solved paper →
  2. March 2025
    What type of statement is the if...else statement ?
    See answer in the solved paper →
  3. March 2024
    Which of the following is used to terminate from the switch statement ?
    See answer in the solved paper →
  4. May 2022
    Statement which is used to make choice between two options and only option is to be performed is written as :
    See answer in the solved paper →
  5. Supplementary 2021
    Statement which is used to make choice between two options and only option is to be performed is :
    See answer in the solved paper →
  6. March 2020
    What is the output of the following PHP code ?
    <?php
    $x;
    if($x==0)
    print "hi";
    else
    print "how are u";
    ?>
    See answer in the solved paper →

About Conditional Statements in PHP

Medium ~60 min study

A PHP script normally runs from the first line to the last. Real pages cannot work that way. A login page must show one message when the password matches and another when it does not. A marks page must print a different grade for 92 and for 45. Conditional statements let the script choose which block of code to run, based on a condition that is either true or false.

Every condition you write uses the PHP operators you met in the introduction to PHP. Comparison operators such as ==, !=, > and <= produce true or false, and logical operators such as && and || join two conditions together. If those operators are not clear to you yet, revise them first, because every question in this chapter depends on them.

Questions from this chapter come as book back MCQs, short answers and syntax questions. The usual pattern is a small script with a given value, say $mark = 65, and you have to say what it prints. You are also asked to write the syntax of each statement with the correct parentheses and curly braces, and to say when switch is a better choice than a chain of elseif.

What you'll learn

  • Explain why a script needs control structures instead of always running line by line.
  • Write a simple if statement with the correct parentheses and braces.
  • Use if-else to choose between two alternative blocks of code.
  • Build an if-elseif-else chain for problems with several ranges or outcomes, such as grading marks.
  • Write a switch statement with case, break and default for matching one value against a list.
  • Trace a given script and state which block runs and what is printed.

Before you start

  • Basic PHP syntax and how PHP code is placed inside the PHP opening and closing tags.
  • Declaring variables, naming rules and PHP data types.
  • PHP operators, especially comparison and logical operators.

Topics covered in this chapter

Control Structures and Control Statements Statements that change the normal top-to-bottom order, letting the flow of control skip or jump to another part of the script.
The if Statement Runs the block inside the curly braces only when the condition in the parentheses is true; otherwise the block is skipped.
The if-else Statement Runs one block when the condition is true and a different block when it is false, so exactly one of the two always runs.
The if-elseif-else Statement Checks several conditions from top to bottom and runs only the block of the first condition that turns out true.
The switch Statement Evaluates one expression and sends control to the case whose value matches it.
break Inside switch Ends the current case and leaves the switch; without it, execution falls through into the next case.
The default Case An optional block in a switch that runs when none of the case values match the expression.

Conditional Statements in PHP explained

How PHP Makes Decisions

Control Structures

By default, statements run in the order they are written. Control structures (also called control statements) change that order, so the flow of control can skip a block or jump to another part of the script. They fall into two groups. Conditional statements decide which block runs. Looping statements, which you study in the next chapter, repeat a block. With these two groups a script can respond to what the user typed, what is in the database, or any other value it checks at run time.

The if and if-else Statements

The if statement is the simplest form. PHP evaluates the condition inside the parentheses. If it is true, the statements inside the curly braces run. If it is false, PHP skips them and moves on. When you need something to happen in the false case too, use if-else. Exactly one of the two blocks runs every time: the if block when the condition is true, the else block when it is false. For example, if ($age >= 18) { echo "Eligible"; } else { echo "Not eligible"; } always prints one of the two messages, never both and never neither.

The if-elseif-else Statement

Use this form when there are more than two possibilities. It starts with an if, followed by one or more elseif parts, each with its own condition, and ends with an optional else. PHP checks the conditions from top to bottom and runs the block of the first condition that is true. The remaining conditions are not checked at all. If none is true, the else block runs. This is the natural choice for ranges, such as grading marks: 90 and above, 75 and above, 50 and above, otherwise fail. Because only the first true condition counts, the order of the tests matters.

The switch Statement

switch is a neater way to compare one variable or expression against a list of fixed values. PHP evaluates the expression once and compares it with each case value in turn. When a case matches, its statements run. Put a break at the end of each case. Without it, PHP carries on into the next case's statements as well, even though that case did not match; this is called fall-through. The optional default block runs when no case matches, much like the final else in an if-elseif-else chain. A menu choice or a day number (1 for Monday, 2 for Tuesday and so on) is a typical switch example.

Common mistakes to avoid

  • Writing a single = inside a condition, as in if ($x = 5). That assigns 5 to $x and the condition is treated as true. Use == to compare values, or === to compare value and type.
  • Leaving out break at the end of a case. PHP then runs the statements of the following cases too, so the output shows extra lines.
  • Forgetting the parentheses around the condition of if or the expression of switch. PHP reports a syntax error.
  • Putting a semicolon straight after the condition, as in if ($x > 10); { ... }. The semicolon ends the if, and the block in braces runs every time.
  • Leaving out curly braces when a block has more than one statement. Only the first statement then belongs to the if; the rest run regardless of the condition.

Test yourself on these with the practice test, then check the worked reasoning in the solved MCQs.

Frequently asked questions

What is the difference between if and if-else in PHP?

A plain if runs its block only when the condition is true and does nothing when it is false. An if-else has two blocks: one for true and one for false. With if-else, one of the two blocks always runs.

When should I use switch instead of if-elseif-else?

Use switch when you compare one variable or expression with several fixed values, such as a menu choice 1, 2, 3 or 4. It is shorter and easier to read than a long elseif chain. When you need ranges or different variables in each test, if-elseif-else is the better fit.

Why is break needed in a switch block?

break ends the matching case and takes control out of the switch. Without it, PHP keeps running the statements of every case below the matching one, whether they match or not, until it reaches a break or the end of the switch.

Can elseif be written as else if in PHP?

Yes. With curly braces, elseif written as one word and else if written as two words behave the same way. The textbook uses elseif, so write it that way in the exam.

What is the purpose of the default case in a switch statement?

default runs when none of the case values match the expression. It works like the final else in an if-elseif-else chain and is used to handle unexpected values, for example printing "Invalid choice" for a menu option that does not exist.

Does switch compare values strictly, and can a case use greater than?

No, switch uses loose comparison, the same as ==, not the strict ===. So a case value of 1 also matches the string "1". A case holds a value, not a comparison. There is a trick of writing switch (true) with conditions in the cases, but for ranges such as marks above 75 an if-elseif-else chain is clearer.

Last updated 15 September 2026

More chapters in Computer Applications

View all
1 Multimedia 2 An Introduction to Adobe PageMaker 3 Introduction to Database Management System 4 PHP: Hypertext Preprocessor 5 Functions and Arrays in PHP 7 Loops in PHP 8 Forms and Files 9 Connecting PHP and MYSQL 10 Introduction to Computer Networks 11 Network Examples and Protocols 12 Domain Name System (DNS) 13 Network Cabling 14 Open Source Concepts 15 E-Commerce 16 Electronic Payment Systems 17 E-Commerce Security Systems 18 Electronic Data Interchange (EDI)