if else statements in php

if statement is the conditional statement. if you want to execute different actions for different conditions. in that case, You can use conditional statements.

There are four ways to use conditional statement in php.

  • if
  • if-else
  • if-else-if
  • nested if

  • The if statement executes some statement if one condition is true.
    Syntax
    if (condition) {
    statement to be executed if condition is true;
    }

    File name : index.php

    $n=10;
    if($n<50){
    echo "$n is less than 50";
    }
    ?>

    Output :-

    10 is less than 50

    if statement

    File name : index.php

    $title = "itechtuto";
    if(isset($title)) {
    $output = $title;
    echo $output;
    }

    if ... else... statement.

    File name : index.php

    $title = "gmaxlifesciences";
    unset($title);
    if(isset($title)) {
    $output = $title;
    echo $output;
    } else {
    $output = "Variable is not initialized";
    echo $output;
    }

    ?>

    if elseif statement.

    File name : index.php

    $title = "gmaxlifesciences";
    $alternate_title = $title;
    unset($title);
    if(isset($title)) {
    $output = $title;
    echo $output;
    } elseif(isset($alternate_title)) {
    $output = $alternate_title;
    echo $output;
    } else($alternate_title) {
    $output = Variable is not initialized;
    echo $output;
    }

    if ... else...

    File name : index.php

    $num = $_POST['$n'];
    if($num > 50)
    {
    echo "Number is larger than 50";
    }
    else
    {
    echo "Number is smaller than 50";
    }
    ?>

    if ... else...

    File name : index.php

    $num = $_POST['$n'];
    if($num > 50)
    {
    echo "Number is larger than 50";
    }
    else
    {
    echo "Number is smaller than 50";
    }
    ?>





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here