Difference Between While And Do While Loops In PHP

admin_img Posted By Bajarangi soft , Posted On 16-09-2020

Difference Between while and do…while Loop

Difference Between While And Do While Loop In PHP

While loop Difference

  • The while loop is the most basic looping structure used in programming.
  • Entry controlled loop.
  • Variable in condition is initialized before the exacution of loop.
  • Single statement, brackets are not required.
  • The codition is specified at the begning od the loop

You need to Try this Example in While loop Method:
First need to HTML doctype 
<!DOCTYPE html>
<html lang="en">
<head>
    <title>CURL Posting Data</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
</body>
</html>

You need to Add is PHP Code while loop method
<?php
$abc = 0;
while($abc <= 5){
    $abc++;
    echo "The number is " . $abc . "<br>";
}

?>

Do..while loop Difference:
  • Statement(s) is executed atleast once, thereafter condition is checked.
  • Exit controlled loop
  • variable may be initialized before or within the loop.
  • Brackets are always required.

You need to Add is PHP Code do..While loop method
<?php
 $abc = 0;
 do{
     $abc++;
     echo "The number is " . $abc . "<br>";
 }
 while($abc <= 5);
?>

Complete Code of Different between while loop and do..while loop in PHP:
index.php
<!DOCTYPE html>
<html lang="en">
<head>
    <title>CURL Posting Data</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>PHP While Loop</h1>
    </div>
    <br>
    <div class="well">
        <?php
        $abc = 0;
        while($abc <= 5){
            $abc++;
            echo "The number is " . $abc . "<br>";
        }

        ?>
    </div>
    <br>
    <div class="text-center">
    <h2>PHP Do...while Loop</h2>
    </div>
    <br>
    <div class="well">
        <?php
         $abc = 0;
         do{
             $abc++;
             echo "The number is " . $abc . "<br>";
         }
         while($abc <= 5);
        ?>
    </div>
</div>
</body>
</html>

Related Post