PHP foreach statement
PHP foreach statement works on PHP array by which we can reach every elements of an array. For example, we have an array like $array(10,20,30,40), we want to print each element of that array. In this kind of case that means in PHP array we use PHP foreach statement. Now take a look at the structure of PHP foreach statement.
foreach($array as $value)In the foreach statement at first we use the actual array and then we use a temporary variable which is used to store the array elements.
{
code to be executed;
}
foreach statement example
Now I am trying to provide an example. Think that we have an array that contains number 1,2,3,4,5 . So now try to print each array value using PHP.<?phpIn the above code we just print the each value of this array. Now we try to print something based on condition. So now take a look at the example.
$number=array(1,2,3,4,5);
foreach($number as $value )
{
echo $value;
}
?>
<?php
$number=array(1,2,3,4,5);
foreach($number as $value )
{
if($value==3)
echo $value;
else
echo $value;
}
?>
Comments
Post a Comment