Get variables from the outside, inside a function in PHP

前端 未结 7 1961
时光说笑
时光说笑 2020-12-03 06:31

I\'m trying to figure out how I can use a variable that has been set outside a function, inside a function. Is there any way of doing this? I\'ve tried to set the variable t

7条回答
  •  春和景丽
    2020-12-03 07:23

    You'll need to use the global keyword inside your function. http://php.net/manual/en/language.variables.scope.php

    EDIT (embarrassed I overlooked this, thanks to the commenters)

    ...and store the result somewhere

    $var = '1';
    function() {
        global $var;
        $var += 1;   //are you sure you want to both change the value of $var
        return $var; //and return the value?
    }
    

提交回复
热议问题