Jay

"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.

Tue, 03/06/2025 - 21:15 Permalink

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <p> <br> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA