0% found this document useful (0 votes)
9 views4 pages

PHP Conditional Statements Overview

This document provides an overview of PHP conditional statements, which are used to control the flow of code based on specific conditions. It details the types of conditional statements, including if, if...else, if...elseif...else, and switch, along with their syntax and examples. Additionally, it highlights common mistakes to avoid and offers practice tasks for better understanding.

Uploaded by

miansunny0303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

PHP Conditional Statements Overview

This document provides an overview of PHP conditional statements, which are used to control the flow of code based on specific conditions. It details the types of conditional statements, including if, if...else, if...elseif...else, and switch, along with their syntax and examples. Additionally, it highlights common mistakes to avoid and offers practice tasks for better understanding.

Uploaded by

miansunny0303
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PHP Conditional Statements Notes

What are Conditional Statements?


Conditional statements allow a program to make decisions and control the flow of code
based on certain conditions.

These are essential when we want our program to execute different code in different
situations, such as:

• Checking if a user is logged in

• Showing different prices based on age or membership

• Handling form validations

Why Use Conditional Statements?


• To control the logic flow

• To react differently to different inputs or states

• To build intelligent and dynamic scripts

Types of Conditional Statements in PHP


1. if statement

2. if...else statement

3. if...elseif...else statement

4. switch statement

1. if Statement
Used when you want to execute a block of code only if a condition is true.

Syntax:
if (condition) {
// code to be executed if condition is true
}

Example:
$age = 20;

if ($age >= 18) {


echo "You are eligible to vote.";
}

2. if...else Statement
Used when you want to execute one block if true, another block if false.

Syntax:
if (condition) {
// if block
} else {
// else block
}

Example:
$marks = 45;

if ($marks >= 50) {


echo "You passed.";
} else {
echo "You failed.";
}

3. if...elseif...else Statement
Used to check multiple conditions, one after another.

Syntax:
if (condition1) {
// code if condition1 is true
} elseif (condition2) {
// code if condition2 is true
} else {
// code if none are true
}
Example:
$score = 85;

if ($score >= 90) {


echo "Grade A";
} elseif ($score >= 75) {
echo "Grade B";
} elseif ($score >= 60) {
echo "Grade C";
} else {
echo "Fail";
}

4. switch Statement
Used when you want to compare one variable against many values.

Syntax:
switch (variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code if no case matches
}

Example:
$day = "Monday";

switch ($day) {
case "Monday":
echo "Start of the week";
break;
case "Friday":
echo "End of the week";
break;
default:
echo "Midweek day";
}

Common Mistakes to Avoid


• Missing or misplaced braces {}
• Forgetting break in switch (causes fall-through)

• Using assignment = instead of comparison ==

Wrong:
if ($x = 5) { ... } // This assigns 5 to $x, not compares

Correct:
if ($x == 5) { ... }

Practice Tasks
1. Write a script that checks if a number is positive, negative, or zero using if...elseif...else.

2. Ask the user for their age and print:

• "Child" if age < 13

• "Teenager" if age between 13–19

• "Adult" if age >= 20

3. Create a switch statement that prints a message based on a selected fruit (apple,
banana, mango).

4. Use if...else to check if a user is logged in (boolean variable) and show appropriate
message.

Summary Table
Statement Type Use Case
if When only one condition to check
if...else When choosing between two paths
if...elseif...else When checking multiple conditions
switch When comparing one variable to many values

You might also like