Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Understanding the Syntax of PHP if Statements
3.
Example
3.1.
PHP
4.
if-else Statements in PHP
4.1.
Syntax
4.2.
Example
4.3.
PHP
5.
 if-else-if Ladder in PHP
5.1.
Syntax
5.2.
Example
5.3.
PHP
6.
Utilizing Nested if Statements in PHP
6.1.
Syntax
6.2.
Example
6.3.
PHP
7.
Frequently Asked Questions
7.1.
Can I use multiple else if blocks in a PHP if-else-if ladder?
7.2.
Is it possible to nest other control structures inside if-else blocks in PHP?
7.3.
What happens if no conditions are true in an if-else-if ladder?
8.
Conclusion
Last Updated: May 27, 2024
Easy

PHP If Else

Author Rahul Singh
0 upvote

Introduction

If-else statements are a fundamental concept in programming that allows you to make decisions based on certain conditions. They help you control the flow of your program by executing different blocks of code depending on whether a condition is true or false. 

PHP If Else

In this article, we will learn the different types of if-else statements in PHP, including their syntax, working and examples to bring better understanding.

Understanding the Syntax of PHP if Statements

The simplest form of decision-making in PHP is the if statement. It checks a condition & if that condition is true, it executes the block of code within the if statement. The basic syntax looks like this:

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

Example

Let’s say you want to display a message if a user's age is over 18. Here's how you could write it:

  • PHP

PHP

$age = 20;

if ($age > 18) {

   echo "You are eligible to vote.";

}
You can also try this code with Online PHP Compiler
Run Code

Output

You are eligible to vote.


In this example, the condition $age > 18 checks whether the age is greater than 18. Since 20 is indeed greater than 18, the message "You are eligible to vote." will be printed on the screen.

if-else Statements in PHP

After learning about the simple if statement, let’s move on to if-else. The if-else structure allows you to define an alternative set of instructions that execute when the if condition fails. This is especially useful when you need to handle two outcomes—when something is true & when it's not.

Syntax

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

Example

Consider a scenario where you need to check if a user is old enough to vote. If they are, you display one message; if not, you display another.

  • PHP

PHP

$age = 16;

if ($age >= 18) {

   echo "You are eligible to vote.";

} else {

   echo "You are not eligible to vote yet.";

}
You can also try this code with Online PHP Compiler
Run Code

Output

You are not eligible to vote yet.


In this code, the if condition $age >= 18 checks if the age is 18 or more. For a user who is 16, the condition fails, & the else part executes, printing "You are not eligible to vote yet."

 if-else-if Ladder in PHP

When you need to evaluate multiple conditions sequentially, the if-else-if structure is your tool of choice in PHP. This allows you to branch out your code execution paths based on different tests, each leading to its own outcome.

Syntax

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

Example

Imagine a website that gives a different greeting based on the time of the day. Here’s how you could implement this with an if-else-if ladder:

  • PHP

PHP

$time = 14; // 14 stands for 2 PM in 24-hour format

if ($time < 12) {

   echo "Good morning!";

} else if ($time < 18) {

   echo "Good afternoon!";

} else {

   echo "Good evening!";

}
You can also try this code with Online PHP Compiler
Run Code

Output

Good afternoon!


In this example:

  • The first condition checks if the time is before noon. If it’s true (before 12), it prints "Good morning!"
     
  • The second condition, within the else if, checks if the time is less than 18 (6 PM). Since 14 is less than 18, it prints "Good afternoon!"
     
  • If neither condition is true, it would proceed to the else clause and print "Good evening!"

Utilizing Nested if Statements in PHP

Nested if statements in PHP allow you to refine your decision-making process by placing one if-else structure inside another. This can be very useful when your decision depends on a series of conditions that must be evaluated in a specific order.

Syntax

if (condition1) {
    // Code executes if condition1 is true
    if (condition2) {
        // Code executes if both condition1 and condition2 are true
    }
}

Example

Suppose you're building a feature for a website that recommends outdoor activities based on the weather. You would check the weather condition first, and if it’s favorable, you then check the temperature.

  • PHP

PHP

$weather = "sunny";
$temperature = 25; // temperature in degrees Celsius

if ($weather == "sunny") {
if ($temperature > 20) {
echo "It's a perfect day for a picnic!";
} else {
echo "It's sunny but a bit chilly. Wear a jacket!";
}
} else {
echo "Not a good day for outdoor activities.";
}
You can also try this code with Online PHP Compiler
Run Code

Output

It's a perfect day for a picnic!


In this code:

  • The outer if checks if the weather is sunny. If true, it proceeds to the nested if.
     
  • The inner if checks if the temperature is greater than 20 degrees. If both conditions are met, it suggests going for a picnic.
     
  • If it’s sunny but the temperature is 20 degrees or less, it suggests wearing a jacket for outdoor activities.

Frequently Asked Questions

Can I use multiple else if blocks in a PHP if-else-if ladder?

Yes, you can use multiple else if blocks to handle various conditions sequentially. This allows you to check for numerous scenarios & react accordingly in your code.

Is it possible to nest other control structures inside if-else blocks in PHP?

Absolutely! You can nest loops like for, foreach, and while within your if-else blocks to perform repeated actions under specific conditions, giving you greater flexibility in how you structure your code.

What happens if no conditions are true in an if-else-if ladder?

If none of the conditions specified in your if-else-if structure are true, the code inside the final else block will execute. If there is no else block, then no action will be taken.

Conclusion

In this article, we have learned about PHP if-else structures, their importance in controlling the flow of a program, & how to use them effectively. Firstly, we discussed simple if statements, expanded to if-else, then moved on to the more complex if-else-if ladder, and finally discussed nested if statements. We discussed every concept with proper examples to show how these structures can be practically applied to enhance the functionality of your web applications. 

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass