close[x]


PHP

PHP-Home PHP-Environment Setup PHP-Syntax PHP-Run PHP in XAMPP PHP-Variable PHP-Comment PHP-Datatype PHP-String PHP-Operators PHP-Decision PHP-loop PHP-Get/Post PHP-Do While loop PHP-While loop PHP-For loop PHP-Foreach loop PHP-Array PHP-Multidimensional Arrays PHP-Associative Arrays PHP-Indexed Arrays PHP-Function PHP-Cookies. PHP-Session PHP-File upload PHP-Email PHP-Data & Time PHP-Include & Require PHP-Error PHP-File I/O PHP-Read File PHP-Write File PHP-Append & Delete File PHP-Filter PHP-Form Validation PHP-MySQl PHP-XML PHP-AJAX



learncodehere.com




PHP - foreach Loop

The foreach loop is used to traverse the array elements. It works only on array and object.

The foreach loop is used to loop through each key/value pair in an array.

In foreach loop, we don't need to increment the value.

Syntax : foreach Loop


foreach ($array as $value) {
  code to be executed;
}

For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element

Let's put these syntax into real use.

Example : foreach Loop


<?php 
$number = array("10", "11", "12", "13","14","15"); 
foreach ($number as $value) {
    echo "Number = $value <br>";
}
?>

This will produce the following result −

Result


 Number = 10
 Number = 11
 Number = 12
 Number = 13
 Number = 14
 Number = 15