xcandiottix Posted August 12, 2010 Share Posted August 12, 2010 I have some variables that I have set with the global command but they don't seem to be working correctly. It's probably my order of operation but not sure. Here's the stripped down version of whats going on: locator() //functions function geoip() global $zip, $city, $areacode, $metrocode, $state, $country, $latitude, $longitude; function locator() geoip() use globals from geoip pass to- ad_display() function ad_display() does some work stat_tracking() function stat_tracking() access global $zip, $city, $areacode, $metrocode, $state, $country, $latitude, $longitude; The problem is once I call stat_tracking() and try to access the global variables, they are all empty. Just to clarify the chain of events is: page loads, calls locator(), locator() calls function geoip() fine then passes work to ad_display(), ad_display() calls stat_tracking(), stat_tracking() now tries to access the globals I made but all variables are blank if I echo them. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/210498-global-var-and-functions/ Share on other sites More sharing options...
xcandiottix Posted August 12, 2010 Author Share Posted August 12, 2010 Got it. after creating global $zip, $city, $areacode, $metrocode, $state, $country, $latitude, $longitude; I needed to call back to these using the format: $city = $GLOBALS['city']; $zip = $GLOBALS['zip']; $areacode = $GLOBALS['areacode']; $metrocode = $GLOBALS['metrocode']; $state = $GLOBALS['state']; $country = $GLOBALS['country']; $latitude = $GLOBALS['latitude']; $longitude = $GLOBALS['longitude']; instead of just using $city, $zip, etc. Quote Link to comment https://forums.phpfreaks.com/topic/210498-global-var-and-functions/#findComment-1098328 Share on other sites More sharing options...
trq Posted August 12, 2010 Share Posted August 12, 2010 global variables are a terrible idea for numerous reasons. They should be avoided at all times. Functions accept arguments and can return data, use this instead. Quote Link to comment https://forums.phpfreaks.com/topic/210498-global-var-and-functions/#findComment-1098331 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.