PHP 7 Conditional Statement: If, Else, Elseif & Switch Case

Laravel

120

w3alert.com

28-12-2019


Conditional Statements PHP

PHP has some important conditional statement like If, if...else, if...elseif...else,. Which are used to test one or more than one condition or expressions. they are also known as making the decision statement.

Types of Conditional Statements in PHP

In PHP, there are the following conditional statements:

  • If Statement
  • If...Else Statement
  • If...Elseif...Else Statement
  • Switch Statement

PHP if Statement

PHP if the statement is used to execute a set of codes If the pass condition is met true.

Syntax:

    if (condition/expression) {
      if the condition is met true then execute a block/group of code
    }

The following example for understanding PHP If Statement:

<!DOCTYPE html>
<html>
<body>

<?php

$dayName = date('D'); // get current day name in php or A textual representation of a day (three letters)
echo "today day name:- ".$dayName;
echo "<br>";

if ($dayName == "Sat") {
    echo 'get current day name in php'; 
}
?>
 
</body>
</html>

PHP If...Else Statement

The PHP if...else statement is used to execute one group of code if the specified condition is met true and another group of code is executed if it is met to false.

Syntax:

    if (condition/expression) {
        if the condition is met true then execute a block/group of code
    } else {
        if the condition is met false then execute a block/group of code
    }

The following example for understanding PHP If...Else Statement:

<!DOCTYPE html>
<html>
<body>

<?php
$dayName = date('D'); // get current day name in php or A textual representation of a day (three letters)
echo "today day name:- ".$dayName;
echo "<br>";
if ($dayName == "Mon") {
    echo 'Condition True'; //
} else {
    echo 'Condition False';
}
?>
 
</body>
</html>

PHP If...Elseif...Else Statement

The PHP if...elseif... else statement used to execute various code groups based on various conditions.

Syntax:

    if (condition1) {
        if the condition1 is met true then execute a block/group of code
    } elseif (condition2) {
        if the condition1 is met false and condition2 is met true then execute a block/group of code
    } else {
        if the condition1 is met false and condition2 is met false then execute a block/group of code
    }

The following example for understanding PHP If...Elseif...Else Statement:

<!DOCTYPE html>
<html>
<body>

<?php

$dayName = date('D'); // get current day name in php or A textual representation of a day (three letters)
echo "today day name:- ".$dayName;
echo "<br>";

if ($dayName == "Mon") {
    echo 'Condition1 True'; 
} else if ($dayName == "Tue") {
    echo 'Condition2 True';
} else {
    echo 'Condition1 and Condition2 are False';
}
?>

 
</body>
</html>

PHP Switch Statement

The switch case statement is similar to If ... Elseif ... Else Statement. which is used to check a variable or an expression against a range of value, then execute a block/group of code.

Syntax:

    switch(expression){

            case value1:

                if the case value1 is met true then execute a block/group of code

            case value2:

                if the case value1 is met false and case value2 is met true then execute a block/group of code

            default: 

                if the case value1 is met false and case value2 is met false then execute a block/group of code

    }


The following example for understand PHP If...Elseif...Else Statement:

<!DOCTYPE html>
<html>
<body>

<?php

$dayName = date('D'); // get current day name in php or A textual representation of a day (three letters)
echo "today day name:- ".$dayName;
echo "<br>";

switch ($dayName) {
    case "Mon":
        echo 'Today is Monday';
        break;
    case "Tue":
        echo 'Today is Tuesday';
        break;
    case "Wed":
        echo 'Today is Wednesday';
        break;
    case "Thu":
        echo 'Today is Thursday';
        break;
    case "Fri":
        echo 'Today is Friday';
        break;
    case "Sat":
        echo 'Today is Saturday';
        break;
  
    default :
        echo "defalut";
}

?>
 
</body>
</html>