Monday, 20 May 2013

php conditional statement


PHP Conditional Statements

    Conditional statements are used to control flow of a program.
    Example -
    if statement, if-else statement, Alternative if-else statement and switch statement
    .

    if statement:

    The if statement is used to execute some code only if a specified condition is true.
    Syntax
    <?php if (condition) { Code to be executed. } "); ?>

    Note
    PHP evaluates the Boolean expression to true or false, if the evaluation result is true, then the code inside the curly braces is executed, else it will be ignored.

    Example

    <?php $x = 5; if($x &lt; 10) // returns true { echo $x; // Will be executed } ?>


    Output
         10

    Example

    <?php $x = 100; if($x &gt; 100) // returns true { echo $x; // Will be executed } ?>
    Output
    Condition is false ,exit from if satement output is blank.

PHP - The if...else Statement

    We use if-else statement to execute some code if a condition is true and another code if the condition is false.

    if statement:

    The if statement is used to execute some code only if a specified condition is true.
    Syntax
    if (condition) { Code to be executed if condition is true. } else { Code to be executed if condition false. }

    Note
    PHP evaluates the Boolean expression to true or false, if the evaluation result is true, then the code inside the if curly braces is executed, else the code inside the else curly braces is executed.

    Example

    <?php 

    $x = 5; 

    if($x > 10) 



    echo $x; //print 5 



    else 



    $x += 7; 

    echo $x; // Will print 12 



    ?>


    Output
         if condition $x>10 is true than output = 5 
         if condition $x>10 is false than output = 12

    Using if-else with more than one condition is:

    if (condition) { Code to be executed if condition true. } elseif (condition) { Code executed if above condition false. } // Multiple elseif block can be added here else { Code executed if above condition false. }


    Example

    Below example represents if-else with more than one condition :
    <?php $x = 5; if($x &gt; 10) { echo $x; } else if ($x == 5) { $x += 7; echo $x; } else { $x++; echo $x; } ?>


    Output
         if condition $x>10 is true than output = 5 
         if condition $x == 5 is true than output = 12 
         Otherwise output = 6 

PHP Switch statement:

    The switch statement is wondrous and magic.
    With the use of the switch statement you can check for all these conditions at once, and the great thing is that it is actually more efficient programming to do this.

    PHP switch statement example

    In our example the single variable will be $day and the cases will be: sunday, munday, tuesday, wednesday, thursday, friday, saturday and the sunday.
    PHP CODE :
    <?php 

    $day = "tuesday"; 

    switch ($day) 



    case "sunday": 

    echo "today is sunday"; 

    break; 

    case "monday": 

    echo "today is monday"; 

    break;

    case "tuesday": 

    echo "today is tuesday"; 

    break;

    case "wednesday": 

    echo "today is wednesday"; 

    break; 

    case "thursday": 

    echo "today is thursday"; 

    break; 

    case "friday": 

    echo "today is friday"; 

    break; 

    case "saturday": 

    echo "today is saturday"; 

    break; 

    default : 

    echo "invalid entry"; 

    break;



    ?>


    Output
         if value of variable $day is "sunday" then output is : today is sunday
         if value of variable $day is "monday" then output is : today is monday
         if value of variable $day is "tuesday" then output is : today is tuesday
         if value of variable $day is "wednesday" then output is : today is wednesday
         if value of variable $day is "thursday" then output is : today is thursday
         if value of variable $day is "friday" then output is : today is friday
         if value of variable $day is "saturday" then output is : today is saturday
         if value of variable $day does not match any case then execute defauly output is : invalid entry

No comments:

Post a Comment