PHPGurukul

Conditional Statement

Conditional statements are the set of commands used to performed different actions based on different conditions.
In PHP we have the following

  • if
  • if else
  • else if
  • switch
  • if statement
    if structure is used for conditional execution of code segment.
    Syntax-

    if(expr)
    {
    statements
    }

    Example-

    <?php
    if($c>$d)
    {
    echo "c is greater than d";
    }
    ?>

    In the above example ,only if the condition “$c>$d” is true, the message “”c is greater than d” is displayed.
    Else Statement
    The conditional statement  “else”  is used as extension of “if” statement. If the condition fails then it executes another statements under the “else” condition.
    Syntax-

    if(expr)
    {
    statements
    }//true
    else
    {
    statements
    }//false

    Based on the result of expressions, the statements are executed.
    Example –

    <?php
    $c=10;
    $d=20;
    if($c>$d)
    {
    echo "c is greater than d";
    }
    else
    {
    echo "d is greater than c";
    }
    ?>

    Result – d is greater than c
    In the above example in the if condition “$c>$d” is true then the message “c is greater than d” is displayed , else the message “d is greater than c” is displayed.
    Else if Condition
    else if condition is used as extension of “if” structure if the condition fails then it executes another “If” condition to execute the code segment under the “else if” statement.
    Syntax –

    if(expr 1)
    {
    statements
    }// true
    elseif(expr 2)// false
    {
    statements
    }// true
    else
    {
    statements
    }// false

    Based on the failure “expr 1” condition, “expr 2” is checked and then the statements are executed.
    Example –

    <?php
    $c=10;
    $d=10;
    if($c>$c)
    {
    echo "c is greater than d";
    }
    elseif($c==$d)
    {
    echo "c is equal to d";
    }
    else
    {
    echo "d is smaller than c";
    }
    ?>

    Output- c is  equal  to d
    In the above example the if the condition “$c>$d” is true then the message “c is greater than d” is displayed, else the condition in the else if that is “$c==$d” is evaluated if it is true the message “c is equal to d” is displayed otherwise d is smaller than c is displayed.
    Switch Statement
    The switch case statement is used to compare a variable or expression values based on which a set of code executed.
    Syntax-

    switch(variable or expression)
    {
    case (value 0)
    {
    statements
    }
    case (value 1)
    {
    statements
    }
    ..............
    case (value n)
    {
    statements
    }
    default
    {
    statements
    }
    }

    Example-

    <?php
    $c=3;
    switch($c)
    {
    case 0:
    echo "value of $c=0<br>";
    break;
    case 1:
    echo "value of $c=1<br>";
    break;
    case 2:
    echo "value of $c=2<br>";
    break;
    default:
    echo "value of $c=Default value <br>";
    break;
    }
    ?>

    Output-  value of 3 = Default value
    In the above example based on the value of $c the messages are displayed of that particular case which matches. The default case accepts anything not matched by other cases, so the value “3” displays the default message.

    Anuj Kumar

    This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
    I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
    .

    Recommended Tutorials for you


    Add comment

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Web Hosting with a FREE domain. On sale ₹99.00/mo

    Follow us

    Don't be shy, get in touch. We love meeting interesting people and making new friends.