PHP If…Else Statements: A Complete Guide with Examples

Post Date: October 26, 2018
Last Updated: October 01, 2026

Introduction

Conditional statements in PHP allow developers to execute different blocks of code depending on whether a condition is true or false. They are essential for controlling application logic, validating user input, handling decisions, and building dynamic web applications.

PHP provides if, if...else, if...elseif...else, and switch statements to handle different conditions.

In this guide, you’ll learn how to use PHP if…else statements with simple examples and practical explanations.

Prerequisites

Before getting started, make sure you have:

  • Basic knowledge of PHP syntax and variables.
  • PHP installed on your system or access to a PHP-enabled web server.
  • A code editor such as Visual Studio Code.
  • Basic understanding of comparison operators such as ==, ===, >, <, and !=.

1. PHP if Statement

The if statement executes a block of code only when a specified condition evaluates to true.

Syntax

if (condition) {
    // Code to execute when the condition is true
}

Example

<?php
$day = date("D");

if ($day === "Fri") {
    echo "Have a nice weekend!";
}
?>

Explanation

  • date("D") returns the abbreviated weekday name, such as Mon, Fri, or Sun.
  • The condition checks whether $day is equal to "Fri".
  • If the condition is true, PHP displays the weekend message.
  • If the condition is false, nothing is displayed.

2. PHP if…else Statement

The if...else statement executes one block of code when the condition is true and another block when it is false.

Syntax

if (condition) {
    // Code when the condition is true
} else {
    // Code when the condition is false
}

Example

<?php
$day = date("D");

if ($day === "Fri") {
    echo "Have a nice weekend!";
} else {
    echo "Have a nice day!";
}
?>

Explanation

  • If the current day is Friday, the first message is displayed.
  • Otherwise, the else block executes and displays the second message.

This structure is useful when an application needs to choose between two possible outcomes.

3. PHP if…elseif…else Statement

The if...elseif...else statement is used when you need to evaluate multiple conditions and execute the corresponding block of code.

Syntax

if (condition1) {
    // Code if condition1 is true
} elseif (condition2) {
    // Code if condition2 is true
} else {
    // Code if all conditions are false
}

Example

<?php
$day = date("D");

if ($day === "Fri") {
    echo "Have a nice weekend!";
} elseif ($day === "Sun") {
    echo "Have a nice Sunday!";
} else {
    echo "Have a nice day!";
}
?>

Explanation

  • The first condition checks whether the day is Friday.
  • If it is not Friday, PHP checks whether the day is Sunday.
  • If neither condition is true, the else block executes.

PHP evaluates conditions in order and executes the first matching block.

4. Using Multiple Statements Inside if…else

When you need to execute multiple statements for a condition, enclose them within curly braces {}.

Example

<?php
$day = date("D");

if ($day === "Fri") {
    echo "Hello!<br>";
    echo "Have a nice weekend!<br>";
    echo "See you on Monday!";
} else {
    echo "Have a nice day!";
}
?>

Explanation

When the current day is Friday, all three statements inside the if block execute. On other days, PHP displays the message in the else block.

Using curly braces consistently makes code easier to read and maintain.

5. Practical Example: Check Whether a Number Is Positive, Negative, or Zero

You can use conditional statements to handle common application logic.

Example

<?php
$number = -5;

if ($number > 0) {
    echo "The number is positive.";
} elseif ($number < 0) {
    echo "The number is negative.";
} else {
    echo "The number is zero.";
}
?>

Output

The number is negative.

This example checks a number against multiple conditions and displays the appropriate result.

6. Common Mistakes to Avoid

When working with PHP if…else statements, keep these points in mind:

  • Using = instead of == or ===: A single equals sign assigns a value rather than comparing two values.
  • Missing curly braces: Although braces can be omitted for a single statement, using them consistently helps prevent logic errors.
  • Incorrect condition order: PHP executes the first matching condition, so arrange conditions carefully.
  • Using incorrect comparison operators: Understand the difference between loose equality (==) and strict equality (===).
  • Forgetting semicolons: PHP statements generally require semicolons at the end.

PHP Official Documentation

For more details about PHP conditional statements, refer to the official PHP documentation:

Conclusion

PHP if…else statements are fundamental to writing dynamic and decision-based applications. By using if, if...else, and if...elseif...else, developers can control how their applications respond to different conditions.

Understanding these structures and practicing with simple examples will help you write cleaner, more maintainable PHP code.

FAQs

1. What is an if statement in PHP?

An if statement executes a block of code only when a specified condition evaluates to true.

2. What is the difference between if and if…else in PHP?

An if statement executes code only when a condition is true. An if...else statement provides an alternative block that runs when the condition is false.

3. Can we use multiple elseif conditions in PHP?

Yes. You can use multiple elseif clauses to check several conditions before reaching the optional else block.

4. What is the difference between == and === in PHP?

The == operator compares values after type conversion when necessary. The === operator compares both value and data type.

Related Article

How to Upgrade from PHP 7.0 to PHP 7.1 on Ubuntu

Talk to Our Experts

Need help with PHP development or web application logic? Our experts can help you build, optimize, and maintain reliable web applications.

Get in touch with us: Contact our team

admin

Our team has expertise across software and web development, WordPress, e-commerce, mobile applications, UI/UX, cloud and infrastructure, DevOps, CI/CD, API integration, security, testing, automation, and technical support. The team also works with AI-based software solutions, LLMs, AI workflows, AI agents, and intelligent application development to help businesses automate processes and build smarter digital solutions. We focus on developing, deploying, maintaining, and optimising secure, scalable, and reliable technology solutions while helping businesses adopt modern technologies and drive digital transformation.

Leave a Reply

Scroll to Top