Difference Between For And Foreach Loops Using PHP

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

Difference Between for and foreach loops In php

Difference Between for and foreach  loops In PHP

For loop Difference:

  • The for loop is an iterative loop which repeats a certain set of code until a specified condition is reached.It is systematically used to execute a set of code for specified number of times, here the number of times denotes the iterator variable.
Initialization:
  • The counter variables, and evaluated once unconditionally before the first execution of the body of the loop.
Condition:
  • The beginning of each iteration, condition is evaluated.
  •  If it evaluates to true, the loop continues and the nested statements are executed.
  •  If it evaluates to false, the execution of the loop ends.
Increment:
  •  it updates the loop counter with a new value. It is evaluate at the end of each iteration.


You need to Try this Example in For 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 For loop method
<?php
for ($xyz=1; $xyz<=5; $xyz++){
    echo "The number is ". $xyz . "<br>";
}
?>
 

Foreach loop Difference:

  • Define the use and it specifically iterate over the elements of the array data structure.

You need to Try is Example in PHP Code Foreach loop method
<?php
 $cars = array('SWIFT','ODI','MARUTI SUZUKI', 'BOLERO');

 foreach ($cars as $value){
     echo $value. "<br>";
 }
?>

Complete Code of Different between For loop and Foreach loop in PHP:
contact.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 For Loop</h1>
    </div>
    <br>
    <div class="well">
        <?php
        for ($xyz=1; $xyz<=5; $xyz++){
            echo "The number is ". $xyz . "<br>";
        }
        ?>
    </div>
    <div class="text-center">
        <h1>PHP Foreach Loop</h1>
    </div>
    <br>
    <div class="well">
        <?php
         $cars = array('SWIFT','ODI','MARUTI SUZUKI', 'BOLERO');

         foreach ($cars as $value){
             echo $value. "<br>";
         }
        ?>
    </div>
</div>
</body>
</html>

Related Post