Conditional Statement On PHP

Hey Developers! Today I want to share conditional statements in PHP, Conditional statements that we have known in other languages are similar to PHP switch codes.

The conditional statement means executing some block of code when the condition is true and not executing when the condition is not true. Or execute some block of code when the condition is false.

There are three different types of Conditional statements or Control statements

  1. If Statement
  2. IF…. else statement
  3. Switch Statement

IF Statement

In the if statement the block of code is executed when the condition is true.

If the condition is false it does not execute a block of code. To execute the block of code first check the condition and then execute it.

The IF statement Example

Execute some code when the condition or statement is true.

Syntax

<?PHP
if(condition)
{
// Execute these block of codes.
}

The above syntax shows the block of code inside that can execute if the condition is true, Otherwise, it does not perform any actions.

Examples

<?php
if(20 >10)
{
echo " 20 is greater than 10";
// logically true 20 is larger than 10 so the
}

Logically true, 20 is larger than 10 so the block of code executes perfectly.

IF…. else statement

In the if … else control statement some block of code is executed if the condition is true. and if the condition is false another block of code is also executed.

At a time one block of code must execute. But not both blocks of code. To use if else statement we must remember two conditions are exits.

One statement that performed when the condition is true and the second executes when the condition is false.

Syntax of if … else

<?php
if(conditon)
{
// Block 1
// If condition is true these block is executed.
}
else
{
// Block 2
// If condition is false these block is executed.
}
?>

If the condition is true block 1 is executed and If the condition is false block 2 is executed.

Two blocks do not execute at the same time.

Example of if … else statement

By using if else we can check student grades

On these if else we use simple and known conditions like 1+2 =3

<?php 
$con1= 2;
$con2=5
if($con2 > $con1)
{
echo "We are the great";

}
else 
{
echo " We are working  to be the great.";
}
?>

As the above code the first block is executed. 5 is greater than 2 so the condition is true.

Short Notations of IF or ShortHand of IF

we can write it in one line of code

<?PHP
$a=30;
if(a < 40) 
$b= "Great";
echo $b;
 ?>

The shorthand of if … else

we can write if …else statement on one line of code as follows.

<?PHP
$a = 50;
if($a >40) ? "50 is greater than 40 " :"50 is less than number";
 ?>

This type of operator is called a Ternary operator.

Switch Statements

Switch statements perform different actions based on different conditions. As its name declares switch means change from one state to another based on the conditions.

On the switch statement select one block of code and perform that action as it wants. As a real-life implementation example switch case is a simple basic calculator.

Let’s start with algorithms we want to develop a basic calculator by using a switch case statement.

There are five types of operators, which are

Adding

Subtraction

Multiplication

Modulo

Operators (=)

After that, the above operators perform on two numbers. Number 1 and Number2, both are positive numbers. We can also use a switch case when want to add(+), sub(-),div(/),mult(*) and mod(%)

It takes two numbers from the user and performs simple operations.

Create file calc.ulator.php

<html>
    <head>
        <title>
            Simple calculators.
        </title>
</head>
<body>
    <h2>Simple calculators</h2>
    <form  method="post" name="submit">
    <label for="">Number 1</label>
    <input type="number" placeholder="enter No1" name="no1"> <br>
    <label for="">Operator</label> <input type="text" placeholder="enter op" name="op"> <br>
    <label for="">Number 2</label>
    <input type="number" placeholder="enter No2" name="no2"> <br>

    <input type="submit" value="Add" name="add"/>
</form>
<?php
if (isset($_POST['add'])){

    $num1=$_POST['no1'];
    $num2=$_POST['no2'];
    $result=0;
    $op=$_POST['op'];
    switch($op)
    {
        case "+":
            $result=$num1 + $num2;
            echo "Result = ".$result;
            break;
        case "-":
            $result = $num1 - $num2;
            echo "Result = ".$result;
            break;
        case "*" :
            $result=$num1 * $num2;
            echo "Result = ".$result;
            break;
        case "/":
            $result = $num1 / $num2;
            echo "Result = ".$result;
            break;
        case "%":
            $result = $num1 % $num2;
            echo "Result = ".$result;
            break;
        default : 
        echo "You have entered wrong operatos";
    }
}
?>
</body>
    </html>

The above code shows how to create simple calculators that take three parameter from user and generate other values

In a switch statement, we must understand terms of expression, Default, case, and others. On the above code if all conditions are not true default block is running

the above image shows the result of the script that was written above based on switch case statements.

Summary

In these articles, we cover three main statements and condition statements. How to use if statements, if else statements, and Switch statements, Expression, other logic

Rember:

Follow Telegram group: https://t.me/tobcoders

Youtube Channels: https://www.youtube.com/@borufantube

Author: bbnadmin

Leave a Reply

Your email address will not be published. Required fields are marked *