Introduction :
Loops are one of the main concepts of PHP language. PHP Loop is such a method or solution that we use to do any work quickly or if we want to do some work repeatedly, then that work can be done in one go only through the loop. PHP has a lot of different loops which are used depending on the need. There are 4 types of loops but three types of loops are used in PHP. All the three loops have their own syntax to use and are used according to the condition. Instead of writing very long code every time we gate an array, we use loops, which makes the code shorter, saves time, and gives quicker response.
Different Types Of Loops :
1. For Loop
2. While Loop
3. Do-While Loop
4. Foreach Loop
Out of these four loops, 3 loops are used in PHP which are described below :
For Loop
While Loop
Do-While Loop
For Loop
While Loop
Do-While Loop
For Loop
In PHP For Loop is the is the control flow of the statement for the given condition or block. for loop is a part of function or a part of the whole method which is executed when any condition has to be run according to the minimum condition or statement.
Syntax Of For Loop :
<?php for(condition 1; condtion 2; condition 3) { // block to be executed } ?>
Example : – create seaven days date
index.php
<?php include('database/conn.php'); $today = date('d-m-y'); $dateArr = []; for ($i = 0; $i < 7; $i++) { $date = strtotime($today."+" .$i. "days"); $dateArr[$i] = date('d-m-y',$date); } echo"<pre>"; print_r($dateArr); die(); ?>
OutPut :
While Loop
While loop is like true false statement which executes the statement when the given condition is true. A method is running inside a class, in that method, if any data is needed only at the time of a specific condition, then while loop is used.
Syntax Of While Loop :
<?php while(condition) { // block to be executed } ?>
Example : get data from database using while loop
index.php
<?php include('database/conn.php'); $formObject = 'SELECT * From users'; $result = mysqli_query($conn, $formObject); ?> <!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"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css"> <link rel="stylesheet" href="https://cdn.datatables.net/1.13.1/css/dataTables.bootstrap4.min.css"> <title>Get Data Using While Loop</title> </head> <body> <div class="container mt-5"> <div class="text-center"> <h1>Users Data</h1> </div> <table id="example" class="table table-striped table-bordered" style="width:100%"> <thead> <th>Id</th> <th>Name</th> <th>Email</th> <th>Address</th> <th>City</th> <th>State</th> </thead> <tbody> <?php while ($row = mysqli_fetch_assoc($result)) { if ($result->num_rows > 0) { $a = 1; } ?> <tr> <td><?php echo $a++; ?></td> <td><?php echo $row["name"]; ?></td> <td><?php echo $row["email"]; ?></td> <td><?php echo $row["address"]; ?></td> <td><?php echo $row["city"]; ?></td> <td><?php echo $row["state"]; ?></td> </tr> <?php } ?> </tbody> </table> </div> </body> </html>
OutPut :
Do-While Loop
The do-while loop is similar to the while loop, the difference is that the loop first executes the statement and then checks the condition. If we want to get data according to a condition in a function, then for that we have to execute whatever data we have first, then we have to get it according to a particular condition, then two-while loop is used there.
Syntax Of do-While Loop :
<?php do { // block to be executed } while(condition); ?>
Example : Get data from database using do-while loop
index.php
<?php include('database/conn.php'); $formObject = 'SELECT * From users'; $result = mysqli_query($conn, $formObject); ?> <!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"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.css"> <link rel="stylesheet" href="https://cdn.datatables.net/1.13.1/css/dataTables.bootstrap4.min.css"> <title>Get Data Using do-while Loop</title> </head> <body> <div class="container mt-5"> <div class="text-center"> <h1>Get Data Using Do-While Loop</h1> </div> <table id="example" class="table table-striped table-bordered" style="width:100%"> <thead> <th>Id</th> <th>Name</th> <th>Email</th> <th>Address</th> <th>City</th> <th>State</th> </thead> <tbody> <?php $a = 1; $row = mysqli_fetch_assoc($result); do { ?> <tr> <td><?php echo $a++; ?></td> <td><?php echo $row["name"]; ?></td> <td><?php echo $row["email"]; ?></td> <td><?php echo $row["address"]; ?></td> <td><?php echo $row["city"]; ?></td> <td><?php echo $row["state"]; ?></td> </tr> <?php } while (mysqli_fetch_assoc($result)) ?> <?php ?> </tbody> </table> </div> </body> </html>