If the PHP program shown below is stored in bug.php, then this command
php bug.php
will produce this output when using PHP 7.0.33-0ubuntu0.16.04.1 (cli) ( NTS ) Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies with Zend OPcache v7.0.33-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies
Output:
Initial array
Array
(
[0] => aa
[1] => bb
)
j = 0 line = aa
element 0 is aa
j = 1 line = bb <----------------------------------------------
element 1 is ***bb <----------------------------------------------
Final array
Array
(
[0] => aa
[1] => ***bb
)
Notice the marked lines above. Element $A[1] is "***bb", yet $line - which the foreach should associate with key 1 - is "bb".
Is this a bug, or is there some PHP subtlety I am unaware of?
<?php
$A = array
(
"aa",
"bb",
);
echo "Initial array\n";
print_r($A);
foreach ($A as $j => $line)
{
echo "j = $j line = $line\n";
echo "element $j is {$A[$j]}\n\n";
if ($j == 0)
$A[1] = "***" . $A[1];
}
echo "Final array\n";
print_r($A);
?>