To be able to use a variable within a function that was previously defined outside of the function we do this:
function animal() {
global $favAnimal, $personName;
//more code here
}
But how can we use a variable that is defined within a function outside that function? For example the following doesn’t work:
function animal() {
$animalName = 'tiger';
}
animal();
echo $animalName; //prints nothing
You cannot access another function’s local variables unless that function is higher up the call stack and has passed the variable into the called function. However, any function can add additional variables to the global scope.
It seems like you might be looking for the
returnstatement: