3/26/15

PHP Conditional Statement


PHP lets programmers evaluate different conditions during of a program and take decisions based on whether these conditions evaluate to true of false.

These conditions, and the actions associated with them, are expressed by means of a programming construct called a conditional statement.

PHP supports different types of conditional statements

  1. The if Statement
  2. In if Statements Output will appear when only Condition must be true.
  3. The if-else Statement
  4. if-else statements allows you to display output in both the condition(if condition is true display some message otherwise display other message).
  5. The if-elseif-else Statement
  6. The if-else-if-else statement lets you chain together multiple if-else statements, thus allowing the programmer to define actions for more than just two possible outcomes.
  7. The switch Statement
  8. The switch statement is similar to a series of if statements on the same expression.


    PHP If Statement

    The if Statement is the simplest conditional statements.
    In if  Statements Output will appear when only Condition must be true.
    If Statement works  much like the English language statement, “if X happens, then do Y.”

    Write a program to check even number.(Number entered by user)

    Eg i
     <?php
          if(isset($_GET['save']))
           {
     if($_GET['n']%2==0)
     {
     echo $_GET['n']." is even number";
     }
           }
      
     ?>
     
     <body>
     <form method="get">
        Enter Your number<input type="text" name="n"/><hr/>    
         <input type="submit" value="check number" name="save"/>
      </form>
     </body>
    
     Output :  10 is even number
        Enter Your number 
       
    
    

    in the above example
    we check if statement program
    First we create a textbox and a button in a form using HTML script.
    in this program we check given number is even or not.
    The isset( ) function is used to check existence. $_GET[ ] is used to collect value that is entered by user.
    after this if condition check the number is even. if the number enter by user is even. statement is execute, print even number.

    Illustrate example to check if given number is greater than 0 then show notification
    message number is positive.

    Eg ii
     <?php
     $num=$_POST['n'];
     if($num>0)
      {
        echo $num." is positive number"; 
      }
      
     ?>
     
     <body>
     <form method="post">
        Enter Your number<input type="text" name="n"/><hr/>    
         <input type="submit" value="check number"/>
      </form>
     </body>
    
     Output :  10 is positive number
        Enter Your number 
       
       
    
    

    in the above example
    First we create a textbox and a button in a form using HTML tags.
    in this program we check if number is grater than zero .it show a message “no is positive”.
    The isset() function is used to check existence.$_GET() function is used collect value that is entered by user.
    if the value is grater than 0. then statement will execute and show Message
    Find sum of even number between 1 to 100
    Eg iii
     
     <?php
      
     $sum=0;
     for($i=1;$i<=100;$i++)
      {
      if($i%2==0)
       {
      $sum=$sum+$i;
       }
      }
    
     echo $sum;
     ?>
    
     Output : 2550
    
    
    in the above example
    whole program are in PHP script. Here we want to sum of even no.
    We declare a variable($sum) initialize its value = 0.
    For loop is used to check value from 1 to 100. The condition that declare inside the if check the number is even or not.
    if the no is even, statement will execute and print sum of all even no those exist between 1 to 100.
    Initially $sum value=0. after check the even number condition.it store the even no in variable ( $i) and it sum with ($sum).
    again and again it check the even number condition and calculate the sum.


    PHP If-Else Statement
    The if statement is quite basic because in If statement output will display when condition must be true, if condition is false it display nothing(blank).
    But if-else statements allows you to display output in both the condition(if condition is true display some message otherwise display other  message).
    In English, this statement would read, “if X happens, do Y; otherwise do Z”.

    Here’s a program to check if given number is greater than 0, then show number is positive else number is negative.

    eg i
     <?php
     $num=$_POST['n'];
     if($num>0)
      {
        echo $num." is positive number"; 
      }
     else
     {
        echo $num." is negative number";
     } 
     ?>
    
    <body>
     <form method="post">
      Enter Your number<input type="text" name="n"/><hr/>
      <input type="submit" value="check number"/>
     </form>
     </body>
    
     Output :   -10 is negative number
      
      Enter your number
                   
         
    
    in the above example
    First we create a textbox and a button in a form using HTML tags.
    in this program we check given number is greater than zero if greater than zero , statement is execute show message “number is positive” . else number is negative.
    Through super global variable ( $_POST[ ] ) collect value from HTML script and store in a local variable($num) that is entered by user.
    after this if else condition is execute. if the condition($num > 0) is true execute the if statement body otherwise execute the else body .
    Here user enters the value -10 so the output will display : -10 is negative number.

    Check given number is even or odd

    eg ii
     <?php
      $num=$_POST['n'];
     if($num%2==0)
      {
        echo $num." is even number"; 
      }
     else
     {
        echo $num." is odd number";
     } 
     ?>
     
     <body>
     <form method="post">
        Enter Your number<input type="text" name="n"/><hr/>
         <input type="submit"/>
      </form>
     </body>
    
     Output :   1 is odd number
      
      Enter your number
                   
         
    
    
    in the above example
    First we create a textbox and a button in a form using HTML script.
    in this program we check given number is even or not.
    Using $_POST[ ] collected value that is entered by user.
    after this check if number is entered by user divided by 2 and its modulus is 0 ($num%2==0),
    it means if statements body execute and output will display the given number is even number else given number is odd number.


    PHP Do While Loop

    The do…while Loop executes the statement once and then check the condition.

    Syntax
     do
     {
      code to be executed;
       }
     while (condition);
     
    
    Eg i
      <?php
     $i=1;
     do
       {
       echo "The number is " . $i . "<br>";
       $i++;
       }
     while ($i<=5);
      ?>
    
     
     Output: The number is 1
      The number is 2
      The number is 3
      The number is 4
      The number is 5   
      
    
    In the above example
    variable $i hold value=”1″. first execute the statement inside do.
    after that it check while condition($i<=5).
    So the given statements execute 5 times.

    Write a program to display table of given number.

    Eg ii
      <?php
     @$tab=$_GET['tab'];
     $i=1;
     do
       {
        $t=$tab*$i;
       echo $t." ";
       
        $i++;
       
       }
     while ($i<=10);  
     ?> 
    
     <body>
      <form>
       Enter Your table
       <input type="text" name="tab"><br/>
        <input type="submit" value="Table">
     </form>
     </body>
    
     
     Output: 10 20 30 40 50 60 70 80 90 100 
          Enter your table 
        
    
    
    In the above example
    Create text-box and a button using HTML script. Logic is performed inside PHP script.
    First we collect the value entered by user using $_GET. $i hold value=1.
    to print the table of 10. ($t=$tab*$i) this condition multiply the entered value with $x(initial value) value is increment after every iteration.
    While check ($i<=10). so the while( ) loop executes the statement 10 times.
    Output will generate table of 10.

    Nested   do-while

    Write a program to display more than one table at a time.
    Eg iii
      <?php
     $n=1;
     $i=0;
     $t=0;
     
     do
       {
        do
      {
        $i++;
        
        $t=$i*$n;
        
        echo $t;
        
        while($i<=10)
       } 
       
        $i=0;
       
        $n++;
      
       while ($n<=10); 
       } 
      ?> 
    
    In the above example
    we display more than one table, nested do while loop is used.
    Declare three variable $n hold vale=”1″
    $i hold value=”0″
    $t hold value=”0″


    PHP Foreach Loop

    The foreach Loop is used to display the value of array.

    You can define two parameter inside foreach separated through “as” keyword.
    First parameter must be existing array name which elements or key you want to display.
    At the Position of 2nd parameter, could define two variable: One for key(index)
    and another for value.
    if you define only one variable at the position of 2nd parameter it contain arrays value (By default display array value).
    Syntax
     foreach ($array as $value)
       {
     code to be executed;
       } 
      
    
    For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) – so on the next loop iteration, you’ll be looking at the next array value.
    The following example demonstrates a loop that will print the values of the given array.
    Eg i
      <?php
     $person=array("alex", "simon","ravi");
     foreach ($person as $val)
       {
      echo $val."<br/>";
       } 
     ?>
    
     
    Output : alex 
              simon  
       ravi
     
    
    In the above example
    declare an array variable($person) hold the elements of array. Here we want to print all element of an array without passing index value.
    We used foreach( ) loop. Passing Variable name ($person as $val).
    it means $val collect all elements of an array. Pass $val with echo statement it show all element as output.

    Define colors name and their index

    Eg ii
      <?php
     $color=array("r"=>"red", "g"=>"green","b"=>"black","w"=>"white");
     foreach ($color as $key=>$val)
       {
      echo $key."--".$val."<br/>";
       } 
    
     ?> 
    
    Output :  r--red
      g--green 
      b--black
      w--white
         
    
    In the above example
    $color variable hold the values (“red”,”green”,”black”,”white”) on index(“r”, “g”, “b”, “w” ).
    if we want do display all values with their index then used foreach( ) loop.
    Inside foreach( ) we have passed three arguments array name, index($key) and value($val) separated by “as”.
    Now call the variable $val to display array values and $key for index.
    Eg iii (sum of given array )
      <?php
     $array=array(10,11,12,13,14,15);
     $sum=0;
           foreach ($array as $x)
       {
            $sum=$sum+$x;
       } 
           echo "Sum of given array = ".$sum;
    
     ?> 
    
     Output : Sum of given array = 75 
    
    In the above example
    Declare variable $array hold the elements of an array, variable $sum hold value=0,
    pass( $array as $x) inside foreach( ) loop.
    It call the values of array one by one and make sum ($sum=$sum+$x) till ends of array.
    at last pass $sum with echo statement to display the sum of given array, output will become.

0 comments:

Post a Comment

FIND US ON FACEBOOK

FIND US ON Twitter