I think I stumbled across a bug in PHP. However, to be sure, I am asking here first.
$k=0;
echo preg_replace_callback('/./', function($groups) use ($k) {
return $k++;
}, 'xxxxxx');
Script output: 000000
Expected output: 012345
Am I missing something?
$kis bound to the closure by value, not by reference. So it will always be the same between multiple closure calls.You can also pass it by reference using
&$k. Note that this will also modify the$kvalue outside the closure.