Introduction
Conditional statements let your PHP code make decisions. They allow your program to run different code based on conditions like user input, login status, or values from a database. Without conditionals, every PHP program would behave the same way all the time.
If you’re building real features—like showing different content to logged-in users in WordPress—you’ll use conditional statements constantly.
In this tutorial, you’ll learn how to use if, else, elseif, and switch with simple examples you can practice right away.
Types of Conditional Statements in PHP
PHP provides the following conditional statements:
ifstatementif...elsestatementif...elseif...elsestatementswitchstatement
Let’s understand each one with examples.
1. PHP if Statement (With Example)
The if statement executes code only when the condition is true.
Syntax
if (condition) {
// code to execute
}
Example
$age = 20;
if ($age >= 18) {
echo "You are eligible to vote.";
}
Output:
You are eligible to vote.
If the condition is false, nothing will be printed.
2. PHP if else Statement (With Example)
The if...else statement runs one block if the condition is true, and another block if it is false.
Syntax
if (condition) {
// code if true
} else {
// code if false
}
Example
$marks = 45;
if ($marks >= 50) {
echo "You passed the exam.";
} else {
echo "You failed the exam.";
}
Output:
You failed the exam.
3. PHP elseif Statement (Multiple Conditions)
Use this when you have multiple conditions to check.
Syntax
if (condition1) {
// code
} elseif (condition2) {
// code
} else {
// code
}
Example
$score = 75;
if ($score >= 90) {
echo "Grade A";
} elseif ($score >= 70) {
echo "Grade B";
} elseif ($score >= 50) {
echo "Grade C";
} else {
echo "Fail";
}
Output:
Grade B
4. PHP switch Statement (With Example)
The switch statement is used when you want to compare one variable with multiple values.
Syntax
switch ($variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code
}
Example
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the work week";
break;
case "Friday":
echo "Weekend is near";
break;
default:
echo "Regular day";
}
Output:
Start of the work week
Important Notes
- Conditions usually use comparison operators like
==,!=,>,<,>=,<= - You can also combine conditions using logical operators:
&&(AND)||(OR)
- Always use
{}for clarity and better readability
Using Logical Operators in Conditions
You can combine conditions using logical operators:
<?php
$age = 20;
$isMember = true;
if ($age >= 18 && $isMember) {
echo "Premium access granted.";
}
?>
&&means AND||means OR!means NOT
Real-Life Example
$isLoggedIn = true;
if ($isLoggedIn) {
echo "Welcome back!";
} else {
echo "Please log in.";
}
This type of logic is commonly used in websites and applications.
Difference Between == and === in PHP Conditions
When comparing values inside conditions, prefer strict comparison:
<?php
$userRole = "1";
if ($userRole === 1) {
echo "Admin";
} else {
echo "Not Admin";
}
?>
Using === avoids bugs caused by automatic type conversion.
Common Beginner Mistakes
1. Using = instead of == or ==== assigns a value. == and === compare values.
2. Forgetting curly braces {}
Always use braces for clarity, even for one-line conditions.
3. Overusing switch
Use switch only when comparing the same variable to multiple values.
4. Writing very long conditions
Break complex conditions into smaller variables for readability.
Summary
- Conditional statements control program flow
- PHP supports
if,if...else,elseif, andswitch - They are essential for decision-making in web applications
- Almost every PHP project uses conditional logic
In the next tutorial, we will learn about PHP Loops.
