PHP

PHP Sessions and Cookies

Introduction to Sessions Cookies in PHP

Conditional Statements (if, else, elseif)

Conditional Statements in PHP: if, else, elseif

Conditional statements in PHP are used to control the flow of your code based on certain conditions. These statements allow you to execute different blocks of code depending on whether specific conditions are met. In PHP, the primary conditional statements are if, else, and elseif. Let's explore how to use them.

The if Statement

The if statement is the fundamental conditional statement in PHP. It allows you to execute a block of code if a specified condition is true. The basic syntax is as follows:

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

Example:

$age = 25;

if ($age >= 18) {
    echo "You are an adult.";
}

In this example, the code within the if block will execute because the condition $age >= 18 is true.

The else Statement

The else statement is often used in conjunction with the if statement. It allows you to specify an alternative block of code to execute if the condition in the if statement is false. The syntax is as follows:

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

Example:

$age = 15;

if ($age >= 18) {
    echo "You are an adult.";
} else {
    echo "You are a minor.";
}

In this example, since the condition $age >= 18 is false, the code within the else block will execute.

The elseif Statement

The elseif statement allows you to test multiple conditions sequentially. If the condition in the if statement is false, it moves on to the elseif condition. You can use multiple elseif statements as needed. The syntax is as follows:

if (condition1) {
    // Code to be executed if condition1 is true
} elseif (condition2) {
    // Code to be executed if condition2 is true
} else {
    // Code to be executed if no conditions are true
}

Example:

$age = 25;

if ($age < 18) {
    echo "You are a minor.";
} elseif ($age >= 18 && $age < 65) {
    echo "You are an adult.";
} else {
    echo "You are a senior citizen.";
}

In this example, the first condition is false, so the code within the first elseif block is checked. If it's true, that code executes. If none of the conditions are true, the code within the else block executes.

Nesting Conditional Statements

You can nest conditional statements within each other to create more complex conditions. This allows you to evaluate multiple conditions in a structured way. Here's an example of nested if statements:

$age = 25;
$hasLicense = true;

if ($age >= 18) {
    if ($hasLicense) {
        echo "You are an adult with a driver's license.";
    } else {
        echo "You are an adult, but you don't have a driver's license.";
    }
} else {
    echo "You are a minor.";
}

In this example, the code first checks if the age is 18 or older. If it is, it then checks whether the person has a driver's license or not.

Conditional statements are essential for making decisions in your PHP code. They enable you to create dynamic, interactive applications that respond to changing circumstances. Understanding how to use if, else, and elseif statements is a fundamental skill for PHP developers.