Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sure you can do globals, but of PHP 5.3.0+ has <strong><a href="http://php.net/manual/en/functions.anonymous.php" rel="noreferrer">anonymous functions</a></strong>, so you can also do closures:</p> <pre><code>&lt;?php function a(){ $a = 1; echo "First: $a, "; ++$a; // This is a closure. It can gain access to the variables of a() with the // use option. $b = function() use ($a) { echo "second: $a"; }; $b(); }; a(); // Outputs: First: 1, second: 2 ?&gt; </code></pre> <h2><strong><a href="http://codepad.viper-7.com/uUhgZg" rel="noreferrer">Try it out at this Codepad example</a></strong></h2> <p><br/> or probably more useful:</p> <pre><code>&lt;?php function a(){ $a = 1; echo "First: $a, "; ++$a; $b = function() use ($a) { echo "second: $a"; }; return $b; }; $fun = a(); // $fun is now $b &amp; $b has access to $a! $fun(); // Outputs: First: 1, second: 2 ?&gt; </code></pre> <h2><strong><a href="http://codepad.viper-7.com/rIWZJ6" rel="noreferrer">Try it out at this Codepad example</a></strong></h2> <p>From the docs:</p> <blockquote> <p>Closures may also inherit variables from the parent scope. Any such variables must be declared in the function header. Inheriting variables from the parent scope is not the same as using global variables. Global variables exist in the global scope, which is the same no matter what function is executing. The parent scope of a closure is the function in which the closure was declared (not necessarily the function it was called from). </p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload