PHP 5.6.31 Released

Voting

Please answer this simple SPAM challenge: three minus one?
(Example: nine)

The Note You're Voting On

ljackson at jjcons dot com
7 years ago
appears kwilson at shuttlebox dot net that you may have just made unintended side effect. Note that adding the global $variable to your test function make the closure function echo second rather than first So the anonymous function works as expected with respect to globals.

<?php
    $variable
= "first";

   
$closure = function() {
        global
$variable;

        echo
$variable . "\n";
    };

   
$closure();

    function
test($closure)
    {
        global
$variable; //Note the scope added here
       
$variable = "second";

       
$closure();
    }

   
test($closure);
?>

prints:
first
second

tested with php 5.3.1

<< Back to user notes page

To Top