title: Setting a global variable's value inside an anonymous function
body: <p>I am using <a href="https://github.com/FriendsOfPHP/Goutte" rel="nofollow noreferrer">Goutte</a> to crawl a site. I am trying to set the <code>$node-&gt;text()</code> value to a global variable, <code>$myGlobalVar</code>, inside the <code>-&gt;each(function ($node) {..});</code> anonymous function.</p>
  <pre><code>$myGlobalVar = '';
  
  $crawler-&gt;filter('.the-link-class')-&gt;each(function ($node) {
      if($myGlobalVar == ''){
          print &quot;using first &quot; . $node-&gt;text() . &quot;\n&quot;;
          $myGlobalVar = $node-&gt;text();            
      }
  });
  
  print &quot;myGlobalVar: &quot; . $myGlobalVar . &quot;\n&quot;; # always empty !!!!!!
  </code></pre>
  <p>I have also tried the <code>use(&amp;$myGlobalVar)</code> closure syntax that was suggested in this <a href="https://stackoverflow.com/questions/11420520/php-variables-in-anonymous-functions">answer I found</a>.  When I modify the relevant code to <code>-&gt;each(function use(&amp;$myGlobalVar) ($node) {..</code> I get a syntax error.</p>