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
    • 369
    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.

    Syntax:
       foreach(array_expression as $value)
    
    <?php
      if (isset($_GET['id']) && isset($_GET['product']) && isset($_GET['price'])) 
    	{
    	 $id=$_GET['id'];
    	 $Product=$_GET['product'];
    	 $Price=$_GET['price'];
    	$arr = array('id'=>$id,'prod'=>$Product,'price'=>$Price);
    	$_SESSION['list'][]=$arr;
    	header('Location:cart.php');
    	}
    ?>
    
    <?php 
    foreach($_SESSION['list'] as $value){
    	?>
    	<tr>
    		<td><?php echo  $value['id']; ?></td>
    		<td><?php echo $value['prod']; ?> </td>
    		<td><?php echo $value['price']; ?> </td>
    	<tr> 
    <?php }
    ?>

    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
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: