Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • how does foreach loop exactaly work in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 400
    Comment on it

    The foreach function in php will be used for a easy way to iterate over the loop and it is only used in array and objects or It will give you an error when you try to use it on a variable with different data type.

    1. Syntax:
    2. foreach(array_expression as $value)
    1. <?php
    2. if (isset($_GET['id']) && isset($_GET['product']) && isset($_GET['price']))
    3.     {
    4.      $id=$_GET['id'];
    5.      $Product=$_GET['product'];
    6.      $Price=$_GET['price'];
    7.     $arr = array('id'=>$id,'prod'=>$Product,'price'=>$Price);
    8.     $_SESSION['list'][]=$arr;
    9.     header('Location:cart.php');
    10.     }
    11. ?>
    12.  
    13. <?php
    14. foreach($_SESSION['list'] as $value){
    15.     ?>
    16.     <tr>
    17.         <td><?php echo $value['id']; ?></td>
    18.         <td><?php echo $value['prod']; ?> </td>
    19.         <td><?php echo $value['price']; ?> </td>
    20.     <tr>
    21. <?php }
    22. ?>

    This loop gives and array_expression as a $_SESSION['name'] on every iteration, the value of the current element is assigned to $value. Every loop the value of the current element is assigned to $value. The internal array pointer is increased by one to the next element. The foreach loop runs for the all elements of an array.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: