php tutorial - PHP Foreach Loop - php programming - learn php - php code - php script
- The foreach construct provides an easy way to iterate over arrays.
- foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data types or an uninitialized variable.
- The foreach loop is a variation of the for loop. It will loop through an entire array, performing the specified actions for each value in the array.

Learn PHP - PHP tutorial - For Each loop - PHP examples - PHP programs
php programming Syntax :
foreach ($array as $value)
{
code to be executed;
}
click below button to copy the code. php tutorial - team
- Foreach : The foreach loop runs for all elements of an array.
- $array : $array is the array variable given by array expression.
- $value : $value specifies that for every loop the value of the current element is assigned to $value.

learn php Sample Code : a simple php program
<!DOCTYPE html>
<html>
<head>
<title>Foreach-Loop</title>
</head>
<body>
<?php
$x=array("1","2","3","4");
foreach ($x as $value)
{
echo $value . "<br />";
}
?>
</body>
</html>
click below button to copy the code. php tutorial - team
php for beginners Code Explanation :

- In PHP, $x=array("1","2","3","4") specify that to value of the current array element is assigned to $value.
- Here, foreach ($x as $value) specify an array pointer is moved by one, until it reaches the last array element.
php coding Sample Output :

- Here in this output the array element 1,2,3,4 will be printed until it reaches the last array element “4”.