i am new in PHP and i had a question: how can i access a variable declared in a function from global scope?
function test(){
$x = 6;
$y = 5;
return $x;
}
test();
echo $GLOBALS['y'];
I want to access variable y in global. Thank you!
i am new in PHP and i had a question: how can i access a variable declared in a function from global scope?
function test(){
$x = 6;
$y = 5;
return $x;
}
test();
echo $GLOBALS['y'];
I want to access variable y in global. Thank you!
Either use $GLOBALS['y']
in the function (it's super-global, so it's available anywhere) or define global $y;
in the beginning of the function.
Note: using such global variables is a bad style, better return the value (or modify it by passing it as a by-reference parameter to the function)