If-Else Statements in PHP: A Complete Guide for Beginners

11/19/2025

if-else statements in PHP with examples

Go Back

If-Else Statements in PHP: A Complete Guide for Beginners

Control flow is one of the most important concepts in any programming language. In PHP, if-else statements help you execute different blocks of code based on specific conditions. Whether you're validating form input, controlling user access, or performing logical checks, the if-else structure is essential.

This article explains if, else, elseif, nested if statements, and real PHP examples to help beginners master conditional logic.


if-else statements in PHP with examples

What Are If-Else Statements in PHP?

If-else statements allow PHP to make decisions. They evaluate a condition and execute code only when that condition is true.

In simple words:

  • if = run code when a condition is true

  • else = run code when the condition is false

  • elseif = run code when additional conditions are true


⭐ Basic Syntax of if Statement

<?php
if (condition) {
    // Code to execute if condition is true
}
?>

Example:

<?php
$age = 20;

if ($age >= 18) {
    echo "You are eligible to vote.";
}
?>

⭐ if-else Statement

Used when you want one block of code to run if the condition is true, and another if it is false.

<?php
if (condition) {
    // Executes if condition is true
} else {
    // Executes if condition is false
}
?>

Example:

<?php
$number = 5;

if ($number % 2 == 0) {
    echo "Even Number";
} else {
    echo "Odd Number";
}
?>

⭐ if-elseif-else Statement

Useful for checking multiple conditions.

<?php
if (condition1) {
   // Code for condition1
} elseif (condition2) {
   // Code for condition2
} else {
   // Default code
}
?>

Example:

<?php
$marks = 75;

if ($marks >= 90) {
    echo "Grade A";
} elseif ($marks >= 75) {
    echo "Grade B";
} elseif ($marks >= 50) {
    echo "Grade C";
} else {
    echo "Fail";
}
?>

⭐ Nested if Statements

An if statement inside another if.

<?php
$age = 20;
$citizen = true;

if ($age >= 18) {
    if ($citizen) {
        echo "You are eligible to vote.";
    }
}
?>

⭐ Using Logical Operators in if Conditions

<?php
$age = 25;
$hasLicense = true;

if ($age >= 18 && $hasLicense) {
    echo "You can drive";
}
?>

⭐ Real-World Example: Login Validation

<?php
$username = "admin";
$password = "12345";

if ($username == "admin" && $password == "12345") {
    echo "Login Successful";
} else {
    echo "Invalid Credentials";
}
?>

⭐ Common Mistakes in If-Else Statements

❌ Missing curly braces { }
❌ Using = instead of == for comparison
❌ Unnecessary semicolon after if condition
❌ Writing unreachable else blocks

Example mistake:

if ($a == 10);  // ❌ Wrong: Semicolon ends the if
{
    echo "Always runs";
}

Correct:

if ($a == 10) {
    echo "Runs only if true";
}

Conclusion

If-else statements are the backbone of decision-making in PHP. They help you control the flow of execution and build dynamic, intelligent applications. Once you master basic and nested conditions, you'll be ready to work with loops, functions, and more complex logic.

Table of content

  • Introduction to Apache Hive
  • Hive Architecture and Components
  • Hive Modes
  • Installation and Setup
    • Installing Hive on Linux/Windows
    • Configuring Hive with Hadoop
    • Verifying the Installation
  • Working with Hive Tables
    • Internal (Managed) Tables
    • External Tables
    • Creating Tables in Hive
    • Altering and Dropping Tables
    • Partitioning in Hive
    • Bucketing in Hive
  • HiveQL Basics
    • SELECT Queries
    • Filtering Data with WHERE
    • Sorting and Grouping Data
    • Using Joins in Hive
  • Advanced Hive Concepts
    • Partition Pruning
    • Dynamic Partitioning
    • Query Optimization in Hive
    • Working with Hive Indexes
    • ACID Transactions in Hive
  • File Formats in Hive
    • Text File
    • ORC (Optimized Row Columnar)
    • Parquet
    • Avro
    • Sequence File
  • Hive Functions
    • Built-in Functions (String, Date, Math)
    • Aggregate Functions
    • User-Defined Functions (UDFs)
  • Integrating Hive with Other Tools
    • Hive and Apache Spark
    • Hive and Pig
    • Hive and HBase
  • Hive Interview Questions
  • Best Practices in Hive
    • Performance Optimization
    • Handling Large Datasets
    • Security and Access Control
  • FAQs and Common Errors in Hive
    • Troubleshooting Hive Issues
    • Frequently Asked Questions
  • Resources and References
    • Official Hive Documentation
    • Recommended Books and Tutorials