PHP Conditional Statements: Types and Examples

By Vijay

By Vijay

Image
I'm Vijay, and I've been working on this blog for the past 20+ years! I’ve been in the IT industry for more than 20 years now. I completed my graduation in B.E. Computer Science from a reputed Pune university and then started my career in…

Learn about our editorial policies.
Updated July 4, 2025
Edited by Kamila

Edited by Kamila

Image
Kamila is an AI-based technical expert, author, and trainer with a Master’s degree in CRM. She has over 15 years of work experience in several top-notch IT companies. She has published more than 500 articles on various Software Testing Related Topics, Programming Languages, AI Concepts,…

Learn about our editorial policies.

PHP Conditional Statements are used to perform various actions based on certain conditions. Explore the different Conditional Statements in PHP with simple examples:

In this tutorial, you will learn what a conditional statement is, PHP if, PHP if-else, PHP if-elseif-else, & PHP switch, differences between if-else & switch, and frequently asked questions (FAQs).

Please note that we have used PHP version 7 in all examples.

Let’s begin!

=> In-Depth PHP Tutorials for Beginners

PHP Conditional Statements: Complete Guide

PHP Conditional Statements

What is a Conditional Statement

As the name suggests, a conditional statement is a statement that works on a particular condition.

There are four conditional statements in PHP, as shown in the list below:

  1. if
  2. if-else
  3. if-elseif-else
  4. switch

PHP If Statements

PHP if statement executes the code only if the stated condition is true.

Syntax:

if(condition){ 
//code to be executed if the stated condition is true 
}

A couple of examples are shown below. You can practice these examples by running the following programming code:

Example 1:

<?php
 
$str = "Hello";
 
if($str == "Hello"){
    echo "Hello!!!";
} 
 
?>

Output:

Hello!!!

Note: In the above example, there will be no output if the stated condition is false.

Example 2:

<?php
 
$num = 12.5;
 
if($num > 0){
    echo "A positive number!";
} 
 
?>

Output:

A positive number!

Note: In the above example, there will be no output if the stated condition is false.

Nested If Statements

A nested if statement is an if statement placed inside another one. The inner if statement executes only if the stated condition in the outer if statement is true.

You can practice this example by running the following programming code:

<?php
 
$num = 8;
 
if(is_integer($num) == true){
    if(($num%2) == 0){
        echo "An even number!";
    } 
} 
 
?>

Output:

An even number!

Note: In the above example, there will be no output if one or more stated conditions are false.

PHP If Else Statements

PHP if else statement executes a particular code if the stated condition is true, or executes another code if that condition is false.

Syntax:

if(condition){
//code to be executed if the stated condition is true
}else{
//code to be executed if the stated condition is false
}

A couple of examples are shown below. You can practice these examples by running the following programming code:

Example 1:

<?php
 
$num = 10;
 
if($num == 10){
    echo "Ten!";
}else{
    echo "Not ten!";
} 
 
?>

Output:

Ten!

Example 2:

<?php
 
$num = 25;
 
if($num < 0){
    echo "A negative number!";
}else{
    echo "A positive number!";
} 
 
?>

Output:

A positive number!

PHP If Elseif Else Statements

PHP if elseif else statement is a combination of if and else. It executes different codes for different conditions.

Syntax:

if(condition1){
//code to be executed if condition1 is true
}elseif(condition2){
//code to be executed if condition1 is false and condition2 is true
}elseif(condition3){
//code to be executed if both condition1 &amp;amp;amp;amp;amp;amp; condition2 are false and condition3 is true
...
}else{
//code to be executed if all conditions are false
}

You can practice this example by running the following programming code:

<?php
 
$num = 10;
 
if($num > 15){
    echo "Greater than 15!";
}elseif($num > 5){
    echo "Greater than 5!";
}else{
    echo "None of the above!";
} 
 
?>

Output:

Greater than 5!

PHP Switch Statements

PHP switch statement (also known as PHP switch case statement) executes one statement from many conditions. It is somewhat like if-elseif-else.

It is recommended to use a switch when there are over three conditions to deal with. Furthermore, the switch is slightly faster than if-else as it computes the condition only once. But, if-else computes the condition every time.

Syntax:

switch(expression){
case value1:
	//code to be executed
break;
case value2:
	//code to be executed
break;
...
default:
	//code to be executed if no match
}

Default Keyword

The default keyword is used to perform a task if none of the cases is true. Further, it is optional.

But, if omitted, there will be no task performed within the switch body if none of the cases is true. Therefore, it is recommended to use the default case.

Break Keyword

The break keyword is used to end the execution of switch statements. It is optional too. But, it may lead to endless execution of the code until the end of the switch statement if omitted.

The default case does not need a break.

Let’s see a few examples. You can practice these examples by running the following programming code:

Example 1: With breaks

<?php
 
$colour = "green";
 
switch($colour){
    case "red":
        echo "Stop!";
        break;
    case "yellow":
        echo "Wait!";
        break;   
    case "green":
        echo "Go!";
        break;   
    default:
        echo "Not Found!";
}
 
?>

Output:

Go!

Example 2: With breaks

<?php
 
$colour = "blue";
 
switch($colour){
    case "red":
        echo "Stop!";
        break;
    case "yellow":
        echo "Wait!";
        break;   
    case "green":
        echo "Go!";
        break;   
    default:
        echo "Not Found!";
}
 
?>

Output:

Not Found!

Example 3: Without breaks

<?php
 
$colour = "green";
 
switch($colour){
    case "red":
        echo "Stop!";
    case "yellow":
        echo "Wait!";
    case "green":
        echo "Go!";
    default:
        echo "Not Found!";
}
 
?>

Output:

Go!Not Found!

Example 4: Without breaks

<?php
 
$colour = "blue";
 
switch($colour){
    case "red":
        echo "Stop!";
    case "yellow":
        echo "Wait!";
    case "green":
        echo "Go!";
    default:
        echo "Not Found!";
}
 
?>

Output:

Not Found!

If Else vs Switch

Both the if-else and switch statements are similar to each other. But, there are significant differences too.

The following table shows the difference between the two types:

If-else StatementSwitch Statement
1It will be executed depending on the output of the expression within the if block.It will be executed depending on the choice of the user.
2Execute either if block or else block.Execute one case after another until a break is found or until the end of the switch statement.
3It is used to select among two choices. It is used to select among several choices.
4Evaluation is based on both equality and logic.Evaluation is based only on equality.
5Test multiple data types.Test only characters or integers.
6Relatively difficult to edit.Relatively easy to edit.

Frequently Asked Questions

1. Name the four conditional statements.

They are if, if-else, if-elseif-else, and switch.

2. What is PHP if condition?

It is for the conditional execution of code. If statement executes the code inside the if statement only if the stated condition is true.

3. What is a PHP switch statement?

It is a conditional statement that is used to execute one statement from many conditions. It behaves somewhat like if-elseif-else.

4. Is a switch statement faster than if-else?

Yes, the switch is slightly faster than if-else. Unlike if-else, the switch computes the condition only once.

5. Can we skip the default case in a switch statement?

Yes, we can skip it as it is optional. But, if omitted, there will be no task performed within the switch body if none of the cases is true.

6. Does the default case need a “break” in a switch statement?

No, a break is not needed for the default case.

Conclusion

A conditional statement is a statement that works on a particular condition.

There are four conditional statements in PHP, named if, if-else, if-elseif-else, and switch. It is recommended to use a switch when there are over three conditions to deal with.

PREV Tutorial | NEXT Tutorial

Was this helpful?

Thanks for your feedback!

READ MORE FROM THIS SERIES:



    Leave a Comment