PHP If-Else Condition

Introduction

In programming, decision-making is one of the most important concepts. It allows your program to take different actions based on different conditions. In PHP, conditional statements like if, if-else, and if-elseif-else are used to control the flow of execution.

These conditions help developers create dynamic and interactive web applications. For example, checking whether a user is logged in, validating form data, or displaying content based on user input all rely on conditional statements.

What is a PHP If-Else Condition?

The if-else statement in PHP executes different blocks of code based on whether a specified condition is true or false.

In simple terms:

  • If the condition is true → execute one block
  • If the condition is false → execute another block

Types of Conditional Statements in PHP

  1. if statement
  2. if-else statement
  3. if-elseif-else statement

Syntax

1. if Statement


if(condition) {
   // code to execute if condition is true
}

2. if-else Statement


if(condition) {
   // code if true
} else {
   // code if false
}

3. if-elseif-else Statement


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

Examples:

Example 1: Simple if Condition


<?php
$x = 100;
if($x > 90) {
   echo 'condition is passed';
}
?>

Output:

condition is passed

Example 2: if-else Condition


<?php
$x = 80;
if($x > 90) {
   echo 'condition is passed';
} else {
   echo 'condition is failed';
}
?>

Output:

condition is failed

Example 3: if-elseif-else Condition


<?php
$x = 80;
if($x > 90) {
   echo 'x value is greater than 90';
} elseif($x > 70 && $x < 90) {
   echo 'x value is between 70 to 90';
} else {
   echo 'condition does not match';
}
?>

Output:

x value is between 70 to 90

Example 4: Multiple Conditions


<?php
$age = 20;
if($age >= 18) {
   echo "Eligible to vote";
} else {
   echo "Not eligible";
}
?>

Example 5: Nested if Condition


<?php
$age = 25;
$citizen = true;
if($age >= 18) {
   if($citizen) {
       echo "You can vote";
   }
}
?>

Real-Life Example

Scenario 1: Login System


<?php
$username = "admin";
$password = "1234";
if($username == "admin" && $password == "1234") {
   echo "Login successful";
} else {
   echo "Invalid credentials";
}
?>

Why this is useful:

  • Validates user input
  • Controls access to systems
  • Improves security

Scenario 2: Grading System


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

Note: Used in educational systems to assign grades.

Why it is Used

Conditional statements are widely used in PHP for the following purposes:

1. Decision Making: They allow the program to make decisions based on conditions.

2. Dynamic Content: Used to display different content to users based on logic.

3. Form Validation: Check whether user inputs meet certain criteria.

4. Authentication Systems: Used in login systems to verify credentials.

5. Error Handling: Helps handle different scenarios gracefully.

Common Mistakes

1. Using Assignment (=) Instead of Comparison (==)

Wrong:


if($x = 10)

Note: This assigns a value rather than comparing.

2. Missing Curly Braces


if($x > 10)
   echo "Hello";
   echo "World";

Note: Only the first statement is part of the if.

3. Incorrect Condition Logic


if($x > 70 && $x < 90)

Note: Make sure conditions are logically correct.

4. Not Using else When Needed

Sometimes developers forget to handle the false case.

5. Deep Nested Conditions

Too many nested if statements make code hard to read.

Conclusion

The PHP if-else condition is a fundamental concept that allows developers to control the flow of their programs. Whether you are building a simple script or a complex web application, conditional statements help you make decisions and execute logic effectively.

PHP if else condition – Interview Questions

Q 1: What is the if-else statement in PHP?
Ans: It executes code based on a condition.
Q 2: Syntax of if-else?
Ans: if ($a > $b) { } else { }
Q 3: Can we use multiple else-if conditions?
Ans: Yes, using elseif.
Q 4: What happens if the condition is false?
Ans: The else block executes.
Q 5: Can conditions be nested?
Ans: Yes, nested if-else is allowed.

PHP if else condition – Objective Questions (MCQs)

Q1. The if statement executes a block when the condition is ______.






Q2. Which keyword is used for multiple conditions?






Q3. The else block executes when the if condition is ______.






Q4. Can if statements be nested in PHP?






Q5. Which operator is used to compare values in if?






Related PHP Tutorials