PDA

View Full Version : Allow variables to be accessed outside a function


bubbles19518
05-18-2007, 11:06 PM
So I have a result that is defined out side of a function in a script. Both the result and the function are inside the same page, but the function is unable to access the result. How do I allow the function to use the variable, without defining it inside the function?

GJay
05-18-2007, 11:15 PM
pass it to the function as an argument

Fumigator
05-18-2007, 11:15 PM
You can use the global statement, but it's kind of a no-no (it's bad design generally because it indicates you are not heeding rules of scope).

Better would be to pass the variable to the function.


$var = 'yay';
$funcResult = execFancyFunc($var);
echo $funcResult;

function execFancyFunc($funcVar) {
if ($funcVar == 'yay') {
return "You are so cool!";
}
}