foreachEasy way to iterate over
ARRAYS.
This function works only over
ARRAYS and
OBJECTS.
This build has common use in PHP, being used to iterate over
ARRAYS.
An error will be issued if you try to use it on a variable with a different data type or an uninitialized variable.
The behavior foreach loops are the most complex loops in PHP.
Since the PHP 7.0.0, foreach does not use the internal array pointer.
You can directly modify the elements of an ARRAY, within a loop, by assigning a value passed by reference.
In this case, in the loop, $value must be prefixed by the & character, however, some care must be taken, otherwise unexpected results may be obtained.
The foreach function does not support the ability to suppress error messages by using the @ symbol.
FIRST SYNTAX TYPE
In the following, we will show the FIRST SYNTAX TYPE of - foreach - conditional structure:
<?php
// STRUCTURE #1
foreach ( array_expr as $value )
statement
?>
SECOND SYNTAX TYPE
In the following, we will show the SECOND SYNTAX TYPE of - foreach - conditional structure:
<?php
// STRUCTURE #2
foreach ( array_expr as $value )
{
statement
}
?>
THIRD SYNTAX TYPE
In the following, we will show the THIRD SYNTAX TYPE of - foreach - conditional structure:
<?php
// STRUCTURE #3
foreach ( array_expr as $key => $value )
statement
?>
FOURTH SYNTAX TYPE
In the following, we will show the FOURTH SYNTAX TYPE of - foreach - conditional structure:
<?php
// STRUCTURE #4
foreach ( array_expr as $key => $value )
{
statement
}
?>
EXERCISE
<?php
$ar01 = array(1 => 'Sunday',
2 => 'Monday',
3 => 'Tuesday',
4 => 'Wednesday',
5 => 'Thursday',
6 => 'Friday',
7 => 'Saturday');
foreach($ar01 as $vr01)
echo $vr01 . '<br>';
?>
RESULT
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
EXERCISE
<?php
$ar02 = [ 1 => 'Sunday',
2 => 'Monday',
3 => 'Tuesday',
4 => 'Wednesday',
5 => 'Thursday',
6 => 'Friday',
7 => 'Saturday'];
foreach($ar02 as $k02 => $vr02)
echo $k02 . ' ' . $vr02 . '<br>';
?>
RESULT
1 Sunday
2 Monday
3 Tuesday
4 Wednesday
5 Thursday
6 Friday
7 Saturday
EXERCISE
<?php
$ar03 = ["Countries", "continentes",
["Brasil", "Portugal", "Japan"],
["South America", "Europe", "Asia"] ];
foreach($ar03 as $k3 => $vr03)
echo($k3) , ' ';
echo '<br><br>';
foreach($ar03 as $k3 => $vr03)
echo '<pre>';
var_dump($vr03);
echo '</pre>';
?>
RESULT
Keys:
0 1 2 3
Values:
array(3) {
[0]=>
string(13) "South America"
[1]=>
string(6) "Europe"
[2]=>
string(4) "Asia"
}
EXERCISE
<?php
$ar04 = [ 2, 8, 18, 32 ];
/* - - - - - - - - - - - - - - - - - - - -
Before PHP 5.5.0, referencing $value
is only possible if the iterated array
can be referenced (i.e. if it is a variable).
The following code works only as of PHP 5.5.0:
- - - - - - - - - - - - - - - - - - - - - */
foreach($ar04 as &$val)
{
$val = $val / 2;
}
print_r($ar04);
echo '<br>';
foreach ($ar04 as $k => $val)
{
echo "<br>{$k} => {$val} ";
print_r($ar04);
}
echo '<br><br>';
?>
RESULT
Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 16 )
The key element = 0 is copied to the key element = 3
0 => 1 Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 1 )
The key element = 1 is copied to the key element = 3
1 => 4 Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 4 )
The key element = 2 is copied to the key element = 3
2 => 9 Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 9 )
The key element = 3 has its value repeated with
the same value as the key element = 2.
3 => 9 Array ( [0] => 1 [1] => 4 [2] => 9 [3] => 9 )
Null coalescing assignment operator
As of PHP 7.4.0, we can explore some of the new features.
Here we cover the enhancements around the null coalescing operator, namely the introduction of the null coalescing assignment operator.
EXERCISE
<?php
function listarr($arr)
{
foreach($arr as $k => $v)
echo $k . ' => ' . $v . '<br>';
}
$arr05a = [ 'red', 'green'];
$arr05b = ['blue', 'yellow', 'black'];
$arr05 = [...$arr05a, 'blue', 'yellow', 'black'];
listarr($arr05a);
echo '<br>';
listarr($arr05b);
echo '<br>';
listarr($arr05);
?>
RESULT
0 => red
1 => green
0 => blue
1 => yellow
2 => black
0 => red
1 => green
2 => blue
3 => yellow
4 => black