"Why? I have no idea. My intuition strongly suggests to me that $value should go out of scope after the first foreach loop, but apparently my intuition is wrong."
Your intuition would be correct for most programming languages,
however in PHP regular variables only have two possible scopes: function-scope and global-scope.
There is NO block-scope, NO loop-scope, none of these.
That's why the (reference) variable defined in the loop will live on until the end of the function/global scope - unless you manually unset() it.
Which is exactly why you're supposed to unset() it after the loop.
"Why? I have no idea. My intuition strongly suggests to me that $value should go out of scope after the first foreach loop, but apparently my intuition is wrong."
Your intuition would be correct for most programming languages,
however in PHP regular variables only have two possible scopes: function-scope and global-scope.
There is NO block-scope, NO loop-scope, none of these.
That's why the (reference) variable defined in the loop will live on until the end of the function/global scope - unless you manually unset() it.
Which is exactly why you're supposed to unset() it after the loop.