mindapolis Posted September 9, 2011 Share Posted September 9, 2011 Sorry, this will be a simple question. Say you define two global variables in your functions.php file as: GLOBAL $salesTax = .07; GLOBAL $shipping = 5.00; Those variables would be accessiible in the checkout.php page, right? Link to comment https://forums.phpfreaks.com/topic/246808-global-variable-question/ Share on other sites More sharing options...
ManiacDan Posted September 9, 2011 Share Posted September 9, 2011 This is far too vague to be answered properly. The "global" keyword is not to be used like that, you use it for importing variables INTO functions, not usually for exporting them out into the global scope. That being said, it's almost always the wrong decision anyway. Globals are dangerous and break the flow of your application. Anything that exists in the global scope is automatically available in the global scope (persisting through include files). Things in the global scope are not available inside of functions or classes. Any items a function needs to do its job should be passed into the function as arguments. Any items the function needs to change should be returned by the function OR passed in by-reference so they can be changed inside the function without having to be returned. In general, "global" should not appear in your codebase. It's a hack. -Dan Link to comment https://forums.phpfreaks.com/topic/246808-global-variable-question/#findComment-1267466 Share on other sites More sharing options...
mindapolis Posted September 9, 2011 Author Share Posted September 9, 2011 thanks so much for your help! Link to comment https://forums.phpfreaks.com/topic/246808-global-variable-question/#findComment-1267470 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.