PHP if else Conditional Statements

Today we will talk about what if else statements are in PHP and how to use them. If else is called Conditional Statement, they are used to check Conditions in which condition should be done and in what condition do not.

PHP if else Conditional statements are sets of commands used to perform different actions based on different situations, when you write the code, then you want to do different tasks for different decisions and then you can do this in your code Conditional statement is to be used.

Conditional statements are used to perform different actions on the basis of different situations. PHP allows you to choose what action you should take based on the outcome of the condition.

This situation can be anything you choose, and you can combine the conditions together to do more complex work.

Types of Conditionals

  • if  statement
  • if-else  statement
  • if-elseif-else  statement
  • switch  statement

if Statement

if conditional statement is used to check whether a given status is true or false, if statement is the most powerful statement of all conditional statements, and by the if statement we can control the execution of statements.

Example

<?php
$d = date("G");

if ($d < "22") {
    echo "Welcome to Coding Bot";
}
?>

if-else  Statement

You can execute some code using the if-else statement if if the condition is true then if’s statement will be executed if condition is false then else’s statement will execute.

Example

<?php
$d = date("G");

if ($d < "22") {
    echo "Welcome to Coding Bot";
} else {
    echo "This is a Example";
}
?>

if-elseif-else  Statement (Nested if)

Nested if statements are also similar to if..else statement but in Nested if statements, the second statement can be started within a statement.

Example

<?php
$d = date("G");

if ($d < "22") {
    echo "Welcome to Coding Bot";
} elseif ($t < "25") {
    echo "Hello";
} else {
    echo "Hii";
}
?>

The switch…case statement

Switch-case statement is similar to the if-elseif statement used to check multiple conditions.

Read more….

Example

switch(variable/expression) 
{
case value 3:
statement(s);
break;

case value 4:
statement(s);
break;

case value n:
statement(s);
break;

default:
statement(s);
break;
}