What is the difference between using unset() on a variable within a foreach loop and using unset() on a reference to that variable?
When using unset() on a variable within a foreach loop, it only removes the value of the variable within the loop iteration and does not affect the original array. However, when using unset() on a reference to that variable, it removes the value from the original array as well.
// Using unset() on a variable within a foreach loop
$array = [1, 2, 3, 4, 5];
foreach ($array as $value) {
unset($value);
}
print_r($array); // Output: [1, 2, 3, 4, 5]
// Using unset() on a reference to that variable
$array = [1, 2, 3, 4, 5];
foreach ($array as &$value) {
unset($value);
}
print_r($array); // Output: []