Loading...
Loading...
00:00:00

Conditional Statements in PHP

In PHP, there are three main types of conditional statements: if, else, and elseif. These statements are used to control the flow of a program based on the conditions specified within them.

if statement

  1. if statement: The if statement is the simplest type of conditional statement. It allows you to execute a block of code if a certain condition is true. The syntax of an if statement is as follows:
if (condition) {
    // code to be executed if condition is true
}

Example of if statement :

$age = 18;

if ($age >= 18) {
    echo "You are old enough to vote!";
}

In this example, the if statement checks if the $age variable is greater than or equal to 18. If it is, the message "You are old enough to vote!" is echo to the screen.

else statement

  1. else statement: The else statement allows you to specify a block of code to be executed if the condition specified in the if statement is false. The syntax of an if-else statement is as follows:
if (condition) {
    // code to be executed if condition is true
} else {
    // code to be executed if condition is false
}

Example of else statement:

$age = 16;

if ($age >= 18) {
    echo "You are old enough to vote!";
} else {
    echo "You are too young to vote.";
}

In this example, the if statement checks if the $age variable is greater than or equal to 18. If it is, the first message is echo. If it's not, the else statement is executed and the second message is echoed instead.

elseif statement

  1. elseif statement: The elseif statement allows you to specify multiple conditions to be checked, with a different block of code to be executed for each condition. The syntax of an if-elseif-else statement 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 all conditions are false
}

Example of elseif statement:

$score = 85;

if ($score >= 90) {
    echo "You got an A!";
} elseif ($score >= 80) {
    echo "You got a B.";
} elseif ($score >= 70) {
    echo "You got a C.";
} else {
    echo "You failed the test.";
}

In this example, the elseif statement checks the value of the $score variable and outputs a different message based on the range it falls into. If $score is greater than or equal to 90, the first message is echo. If it's between 80 and 89, the second message is echoed. If it's between 70 and 79, the third message is echoed. If it's lower than 70, the last message is echo.

These are just a few examples of how conditional statements can be used in PHP. They can be used in a variety of situations to control the flow of a program and make decisions based on specific conditions.

It is important to note that these statements can be nested within each other, allowing for more complex conditional logic to be implemented. Additionally, the ternary operator can be used as a shorthand version of an if-else statement, as follows:

$variable = (condition) ? true : false;

This assigns the value true to the variable if the condition is true, and false if the condition is false.

Switch Case in PHP

In PHP, a switch statement is a control structure that allows you to execute different blocks of code based on the value of a variable or expression. It provides a cleaner and more concise alternative to a series of nested if-else statements.

The basic syntax of a switch statement is as follows:

switch (expression) {
    case value1:
        // code to be executed if expression matches value1
        break;
    case value2:
        // code to be executed if expression matches value2
        break;
    // more cases can be added as needed
    default:
        // code to be executed if no case matches expression
        break;
}

Here's an example of a switch statement in action:

$day = "Monday";

switch ($day) {
    case "Monday":
        echo "Today is Monday";
        break;
    case "Tuesday":
        echo "Today is Tuesday";
        break;
    case "Wednesday":
        echo "Today is Wednesday";
        break;
    default:
        echo "It's not Monday, Tuesday, or Wednesday";
        break;
}

In this example, the switch statement checks the value of the $day variable and executes the corresponding block of code. If $day is "Monday", the first case is matched and the message "Today is Monday" is echoed to the screen. If $day is "Tuesday" or "Wednesday", the corresponding cases are matched and their messages are echoed. If $day is any other value, the default case is matched and its message is echoed.

It's important to note that each case must end with a break statement, which tells PHP to exit the switch statement and continue executing the rest of the program. If you forget to include a break statement, PHP will continue executing the code for the next case as well.