Conditional Statements in PHP

Chapter 6 · Tamil Nadu State Board

  • 4 min read
  • 5-question quiz

Every key point below shows the sentence from your textbook it comes from. How we check content

In short

This lesson explains how PHP conditional statements choose which code to run based on a condition. It introduces if, if ... else, if ... elseif ... else, and switch statements. These structures let a script take different paths when conditions or values differ. Understanding them helps students make decisions in PHP programs.

Written by AI from this lesson. How we check it

Key points

  • Control structures can change the order in which script statements run.
    From your textbook
    “Some segments in a script are executed based on a condition.”
  • An if statement runs its code block only when its condition is true.
    From your textbook
    “A block of code is executed if a certain condition is true.”
  • An if ... else statement chooses between a true block and a false block.
    From your textbook
    “It executes one block of code if a condition is true and another block of code if the condition is false.”
  • An if ... elseif ... else statement checks multiple conditions, and only one block runs.
    From your textbook
    “only one of the blocks of code will be executed, depending on which condition is true.”
  • A switch statement checks an expression against cases, and default is used if none match.
    From your textbook
    “The default case is optional, and will be executed if none of the other case statements match the value of the expression.”
  • In a switch statement, break ends a case and exits the switch.
    From your textbook
    “The role of the break statement in a switch statement is to terminate the case and exit the switch statement.”
Test yourself: 5 questions

Formulas in this chapter

  • if statement syntaxif (condition) // code to be executed if condition is true;Explained here
  • if ... else syntaxif (condition) // True-block; else // False-block;Explained here
  • if ... elseif ... else syntaxif (condition1) { // code to be executed if condition1 is true; } elseif (condition2) { // code to be executed if condition1 is false and condition2 is true; } Explained here
  • switch statement syntaxswitch (expression) { case value1: //code to be executed if expression = value1; break; case value2: //code to be executed if expression = value2; break; ... deExplained here

Control Structures and PHP Conditional Statements

Definition

Control statements or Control Structures
Statements that make control jump from one part of a script to another are called control statements or control structures.

A script does not always run its statements in order. A condition can decide which part runs next. The two major categories of control structures are Conditional statements and Looping statements.

Definition

Conditional statements
Conditional statements in PHP run code based on a specific condition.

Types of conditional statements in PHP

  • if statement
  • if ... else statement
  • if ... elseif ... else statement
  • switch statement

if and if ... else Statements

The if statement is the simplest conditional statement. It runs a block of code only when its condition is true. If the condition is false, that code is skipped.

if statement syntax

if (condition) // code to be executed if condition is true;

condition is the test that decides whether the code runs.

Worked example

$x = 10; if ($x > 5) echo “x is greater than 5”;

Try it first, then show the solution
  1. Check the condition $x > 5.
  2. $x is 10, so the condition is true.
  3. The code runs and outputs “x is greater than 5”.

Answer: x is greater than 5

The if ... else statement gives two possible paths. The True-block runs when the condition is true. The False-block runs when the condition is false.

if ... else syntax

if (condition) // True-block; else // False-block;

condition decides whether the True-block or the False-block runs.

Worked example

$x = 10; if ($x > 5) echo “x is greater than 5”; else echo “x is not greater than 5”;

Try it first, then show the solution
  1. Check whether $x > 5.
  2. $x is 10, so the condition is true.
  3. The if block runs, not the else block.

Answer: x is greater than 5

if ... elseif ... else Statements

Use if ... elseif ... else to test several conditions and run code for the matching condition. It starts with if, can have one or more elseif parts, and ends with else. Only one block runs; if all conditions are false, the else block runs.

if ... elseif ... else syntax

if (condition1) { // code to be executed if condition1 is true; } elseif (condition2) { // code to be executed if condition1 is false and condition2 is true; }

Each condition is checked in order. The matching block runs, or else runs if none match.

Worked example

$x = 12; if ($x > 20) echo “x is greater than 20”; elseif ($x > 15) echo “x is greater than 15 but not greater than 20”; elseif ($x > 10) echo “x is greater than 10 but not greater than 15”; else echo “x is not greater than 10”;

Try it first, then show the solution
  1. Check $x > 20. This is false because $x is 12.
  2. Check $x > 15. This is also false.
  3. Check $x > 10. This is true, so its code runs.

Answer: x is greater than 10 but not greater than 15

switch Statements and Key Rules

A switch statement provides multiple branches. It checks an expression against case values and runs the code for a matching case. If no case matches, the optional default block runs.

switch statement syntax

switch (expression) { case value1: //code to be executed if expression = value1; break; case value2: //code to be executed if expression = value2; break; ... de

expression is checked against each case value. break ends a case and exits the switch statement.

Worked example

$x = 10; switch ($x) case 5: echo “x is equal to 5”; break; case 10: echo “x is equal to 10”; break; case 15: echo “x is equal to 15”; break; default: echo “x is not equal to 5, 10, or 15”;

Try it first, then show the solution
  1. The expression $x has the value 10.
  2. The case value 10 matches.
  3. That case runs and break exits the switch statement.

Answer: x is equal to 10

Rules to remember

  • Curly braces { } separate blocks of statements within a control structure.
  • A condition or expression must be enclosed in parentheses.
  • elseif can also be written as two separate words: else if.
  • Without break, execution continues through later case statements until a break is encountered.
  • The default case is optional.

Remember: In a switch statement, break ends the case and exits the switch.

Questions students ask

Short, direct answers to what students search for in this chapter.

What are conditional statements in PHP?

Conditional statements in PHP run code based on a specific condition. The types are if, if ... else, if ... elseif ... else, and switch. They allow a script to choose which block of code to execute.

How does an if statement work in PHP?

An if statement checks a condition. If the condition is true, the code in its block runs. If the condition is false, the code is skipped. The condition must be enclosed in parentheses.

What is the difference between if ... else and if ... elseif ... else?

An if ... else statement chooses between two blocks: one for a true condition and one for a false condition. An if ... elseif ... else statement checks several conditions and runs the matching block. If none are true, its else block runs.

How does a switch statement work in PHP?

A switch statement checks an expression against several case values. It runs the block for a matching case. If no case matches, the optional default block runs. A break statement ends a case and exits the switch.

What does break do in a PHP switch statement?

The break statement terminates a case and exits the switch statement. If break is not used, execution continues through subsequent case statements until a break is encountered.

Practice

Question 1 of 5Score 0

When does an if statement execute its code block?

Exam questions

Practice questions with model answers. These are not from an official paper.

Name the four types of conditional statements in PHP covered in this lesson.2 marks

They are if, if ... else, if ... elseif ... else, and switch statements.

Explain how an if ... else statement works.2 marks

It checks a condition. If the condition is true, it executes the true block; if it is false, it executes the false block.

What happens in an if ... elseif ... else statement when all the conditions are false?2 marks

The code inside the else block is executed.

Explain how a switch statement chooses code and describe the roles of default and break.5 marks

The switch statement is a multiple branching statement in PHP. It transfers control to one of several possible points based on an expression.

  1. Expression and cases: The switch evaluates an expression and checks its value against the values listed in the case statements.
  2. Matching case: When a case value matches the expression, the code in that case block is executed.
  3. Role of break: The break statement terminates the current case and exits the switch statement. Without break, execution continues through later cases until a break is encountered.
  4. Role of default: The optional default case runs when the expression does not match any of the case values.

Example: In the chapter’s example, $x is 10, so case 10 runs and outputs “x is equal to 10”.

Thus, switch selects code by matching the expression to a case, while default handles no match and break exits the switch.

Watch out

  • Do not assume an if block runs when its condition is false; that code is skipped.
  • Do not confuse the roles of elseif and switch: elseif checks conditions, while switch matches an expression against case values.

Still stuck on this lesson?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →