Values & References16 / 16
Up next

The dangling foreach ref

<?php
declare(strict_types=1);
$v = [1, 2, 3];
// after this loop, $x still references the last element
foreach ($v as &$x) {}
// unset breaks the reference and avoids a classic bug
unset($x);
// a later normal loop no longer clobbers $v[2]
foreach ($v as $x) {}
print_r($v);
What you're solving

always unset after a by-ref loop