PHP - Copying an array changes the internal pointer?

There is a strange one:

This code outputs "345":

$a = array(1,2,3,4,5);
foreach ($a as $key => $input)
{
    $ab  = next($a);
    echo $ab;
}

      

But this code outputs "2345":

$a = array(1,2,3,4,5);
$abc = $a;
foreach ($a as $key => $input)
{
    $ab  = next($a);
    echo $ab;
}

      

The only difference between these two codes is that in the second example, the $ a array was duplicated into the $ abc array (line 2)

Does this affect the internal pointer of the array and how?

What is the expected behavior?

+3


source to share


1 answer


It all depends on which version of PHP you are using:

Anything less than 7 will output 345



while version 7 will output 2345

You can check Here

0


source







All Articles