Web Programming TutorialsLearn How to do Changes to Foreach Statement in PHP 7

Learn How to do Changes to Foreach Statement in PHP 7

Foreach-Statement
In this chapter, we are going to discuss the various changes made to the foreach statement in PHP 7, which are backward incompatible to PHP 5.x and other older versions of PHP.

Backward incompatible changes to foreach statement in PHP 7
PHP 7 has undergone some changes with regards to the behaviour of the foreach control structure. All of the changes in the foreach statement are around the internal array pointer handling and the array modification which is being iterated over. The following are the backward incompatible changes made to the foreach statement in PHP to PHP 5.x and its older versions.

  • foreach statement – do not change the internal array pointer: In PHP versions 5.x and before, we were able to modify the internal array pointer while iterating over an array with the foreach statement. However in PHP 7, this feature has been removed completely. Let’s understand this with the help of the following example.
<html>
   <head>
      <title><?php echo "Welcome to PHP-7 Tutorials";?></title>      
   </head>
   <body>
<?php
$array = [10, 20, 30, 40];
foreach ($array as &$val) {
    var_dump(current($array));
}
?>  
   </body>
</html>

Explanation of the PHP 7 code.

  • Firstly, we are declaring an array which has 4 element [10, 20, 30, 40].
  • Next, we are using the foreach statement to iterate over the array, and inside the loop we are displaying information about the variable using the var_dump function.
  • Inside this function, we are using the current function that has the array as input. The current function is used to return the current element in an array.

Output
When we execute the above PHP 7 program on the web server, then we observe that we are unable to modify the internal array pointer as shown below.
Output1

  • foreach statement (by-value) operates on an array copy: In PHP 7, if we are using the foreach statement in the default by-value mode, then it will operate on an array copy which is being iterated over instead of the actual array itself. This makes sure that the changes that we made to the array during its iteration will not affect the values of the actual array which are iterated over.
  • foreach statement (by-reference) has an improved iteration behaviour: PHP 7 has released an improved version of the foreach statement in terms of its iteration behaviour. Following this improvement, now foreach statement when iterated by-reference over an array is capable of tracking the changes that are made to the array. In PHP 5.x and earlier version, the foreach statement ignored the newly appended elements to the array while iterating over it and do not display them. Let’s understand it in a better way with the help of the following example.
<html>
   <head>
      <title><?php echo "Welcome to PHP-7 Tutorials";?></title>      
   </head>
   <body>
<?php
$array = [10,20,30];
foreach ($array as &$val) {
	var_dump($val);
	$array[2] = 99;
	$array[3] = 40;
}
?> 
   </body>
</html>

Explanation of the PHP code

  • Firstly, we declared an array which has 3 elements [10, 20, 30].
  • Next, we used the foreach statement to iterate over the array, and inside the loop we displayed the information about the variable using the var_dump function.
  • Inside this function, we used the current reference pointer value to display the array values.
  • Next, we updated the array at index 2 from 30 to 90 while iterating by reference over the array by using foreach statement.
  • Lastly, we have appended a new element (40) at index 3 while iterating by reference over the array by using foreach statement.

Output
When we execute the above PHP 7 program on the web server then we observe that the foreach statement has displayed the updated element value at index 2 along with the newly added element at index 3 as shown below. This shows the improved iteration behaviour of the foreach statement.
Output2

  • foreach statement – Iteration of (non-Traversable) objects: In PHP 7, iterating over the non-Traversal objects as well as arrays by-reference possess the same behaviour. It has an improved behaviour, as it is capable of displaying the modified array elements which were modified during the iteration using foreach by-reference method. This is applicable to the (non-Traversable) objects as well when properties are added to or removed from the objects. Let’s understand this with the help of following example:
<htm
l>
   <head>
      <title><?php echo "Welcome to PHP-7 Tutorials";?></title>      
   </head>
   <body>
<?php
$inputArray = array(10, 20, 30);
$myobject = (object)$inputArray;

if ( !($inputArray instanceof \Traversable) ) {
	print "inputArray is NOT Traversable";
}
if ( !($myobject instanceof \Traversable) ) {
	print("<br />");
	print "myobject is NOT Traversable";
}
print("<br />Inside Array Iteration<br />");
foreach ($inputArray as &$val) {
	print $val;
	$inputArray[3] = 40;
	print("<br />");
}
print("<br />Inside Object Traversing<br />");
foreach ($myobject as &$val) {
	print $val;
	print("<br />");
}
?> 
   </body>
</html>

Explanation of the PHP code
The above PHP 7 code does the following.

  • Firstly, we declared an array that has 3 elements and a non-Traversable object similar to that of an array.
  • Next, we checked an array and the non-Traversable object against non-traversable behaviour.
  • Next, we iterate over an array by reference using foreach statement. Here, in between we are appending an element as 40 at array index 3.
  • Lastly, we are Iterating over a non-Traversable object by reference using the foreach statement.

Output
When we execute the above PHP 7 program on the web server, then we can see how it reacts. Here, we can observe if the behaviour of the foreach statement for an array and non-Traversable objects are similar.
Output3
Source Code: – Changes to foreach statement in PHP 7
Conclusion
In this chapter, we discussed about the various changes made to the foreach statement in PHP 7 along with practical examples.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -