Javascript closures vs PHP closures, what's the difference?
What are the differences between closures in JS and closures in PHP? Do they pretty much work the same way? Are there any caveats to be aware of when writing closures in PHP? Stefan Gehrig One difference is how both cope with storing the context in which an anonymous function is executed: // JavaScript: var a = 1; var f = function() { console.log(a); }; a = 2; f(); // will echo 2; // PHP $a = 1; $f = function() { echo $a; }; $a = 2; $f(); // will result in a "PHP Notice: Undefined variable: a in Untitled.php on line 5" To fix this notice you'll have to use the use syntax: $a = 1; $f = function