let's say I have this: $coke=1; $coffe=1; function Bar() { echo "This is Bar. I have $coke cokes and $coke coffes"; } Watt's the corect way to ask $coffe and coke from inside the function?
Sending it is the correct way to do it. Other ways would be to use globals or store them in a superglobal (like sessions): global $coke,$coffee; $coke=$coffee=1; function Bar() { global $coke, $coffee; echo "This is Bar. I have $coke cokes and $coffee coffee"; } Bar(); Code (markup):
Or ($_GLOBALS, without setting global $coke use $_GLOBALS['coke']) or if you want you can use $_SESSION But for the shorthand global $coke is easier http://www.nqbao.com/2009/11/the-best-way-to-access-PHP-global-variable
It would be better if you DIDN'T mention that method, given that it's a security nightmare which is why it's been disabled by default for christmas only knows how long, deprecated as of 5.3 and doesn't even EXIST in PHP 5.4+ !!! Hence another of those nice big red warning boxes: http://php.net/manual/en/security.globals.php Much like mysql_ functions we've been told for eight to ten years to STOP DOING THAT; to the point they're having to FINALLY shove it down people's throats. I would warn that in general, globals are not a reliable or secure way to be passing data around -- while sometimes they're the best answer to a problem, generally speaking you're better off just doing a Nancy Reagan on them. "Just say no!" Using objects to isolate scope is one of the better answers; or singletons with getters/setters so that it can't be set/rewritten by the wrong things... though that could be overboard if all you are doing is top-down execution.
I agree, but the TS wants it that way... so i gave him the way I still think that function ($var, $var1) is the best method!