Practical 3 | PHP (22619) | MSBTE Lab Manual Answers| Write a PHP program to demonstrate the use of Looping structures using- a) While statement b) Do-while statement c) For statement d) Foreach statement

 Web Based Application Development with PHP

(22619) Answers



 Write a PHP program to demonstrate the use of Looping structures using- a) While statement b) Do-while statement c) For statement d) Foreach statement

* Program Code 
1) Write a program to print first 30 even numbers. (Using for, while, do..while)
Ans=> 

Using For Loop



  1. <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>First 30 Even Numbers</title>
    </head>
    <body>
        <?php
        for($i=0;$i<=60;$i++)
        {
            if($i%2==0){
                echo $i,"<br>";
            }
        }
        ?>
       
    </body>
    </html>
using while loop

  1. <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>First 30 Even Numbers</title>
    </head>
    <body>
        <?php
         $i=1;
         while($i<=60)
         {
            if($i%2==0)
            {
                echo $i,"<br>";
               
            }
         }
       
        ?>
       
    </body>
    </html>

using do while loop
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>First 30 Even Numbers</title>
</head>
<body>
    <?php
     $i=1;
     do{
       if($i%2==0)
       {
        echo $i,"<br>";
        $i++;
       }
     } while($i<=60);
    ?>
   
</body>
</html>

* Exercise.

1)Write any program using if condition with for loop.

  <?php
    for($i=0;$i<=60;$i++)
    {
        if($i%2==0){
            echo $i,"<br>";
        }
    }
    ?>


2)Write a program to display pyramids of star/patterns using increment/decrement.



  1. <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Star Pattern </title>
    </head>
    <body>
    <?php
       
        $size = 5;
        for($i = 0; $i < $size; $i++) {
           
            for($j = 0; $j <= $i; $j++) {
                echo "*";
            }
            echo "<br>";
        }
    ?>
       
    </body>
    </html>

* Practical Related Questions.

1)When for loop will be terminated?
Ans => 1) When the break statement is encountered inside a loop, the loop is immediately terminated, and the program control resumes at the next statement following the loop
             
2) Difference between for and for-each loop
Ans=>             for                                                      for each 
         1)   The iteration is clearly visible            The iteration is hidden.

         2) Good Performance                                Better Performance

        3) Stop condition is specify                        stop condition has to be explicitly specify

        
.
.
.
.
Keep Growing ❤   
        

Post a Comment

0 Comments