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 to "global" but it doesn't seems to work out as expected.
A simple example of my code
$var = '1';
function() {
$var + 1;
return $var;
}
I want this to return the value of 2.
Technically the global keyword is used inside a function, not outside.
But you shouldn't really use this approach. See Why global variables are evil. Instead, pass a variable as a parameter:
function($param) {
$param += 1;
return $param;
}
$var = 1;
$var = function($var); // get your value of 2