I have the following php functions.
function a() {
$var = "variable";
return $var;
}
function b() {
$arr = array('a','r','r','a','y');
return $arr;
}
And some more PHP out of those functions.
$var = a();
$arr = b();
print_r($var);
print_r($arr);
$var and $arr are defined in functions, but then are redefined outside of the functions. How do I make it so that the variables and arrays out of the functions are separate from the ones that are in the functions, so that the variables and arrays in the function don’t exist out of the function?
Functions have their own scope. Your function variables do not exist outside of the function unless they are defined using the
globalkeyword. You may be confusing yourself by using the same variable names both inside and outside of the functions. Try changing the function vars to$fx_arrand$fx_var, respectively. You will see that they do not exist outside of the functions.