karthikanov24 Posted October 27, 2009 Share Posted October 27, 2009 hi In the config file of my application,i have the following code, <?php require_once 'database.php'; require_once 'common.php'; // get the shop configuration ( name, addres, etc ), all page need it $shopConfig = getShopConfig(); ?> In the common.php file,i have the following codes: <?php require_once 'config.php'; require_once 'database.php'; function getShopConfig() { // get current configuration $sql = "SELECT sc_name, sc_address, sc_phone, sc_email, sc_shipping_cost, sc_order_email, cy_symbol FROM tbl_shop_config sc, tbl_currency cy WHERE sc_currency = cy_id"; $result = dbQuery($sql); $row = dbFetchAssoc($result); if ($row) { extract($row); $shopConfig = array('name' => $sc_name, 'address' => $sc_address, 'phone' => $sc_phone, 'email' => $sc_email, 'sendOrderEmail' => $sc_order_email, 'shippingCost' => $sc_shipping_cost, 'currency' => $cy_symbol); } else { $shopConfig = array('name' => '', 'address' => '', 'phone' => '', 'email' => '', 'sendOrderEmail' => '', 'shippingCost' => '', 'currency' => ''); } return $shopConfig; } function displayAmount($amount) { global $shopConfig; return $shopConfig['currency'] . number_format($amount); } Here inside the funtion displayAmount(),why $shopconfig is declared as global?whats the use of it..? thanks karthikanov24 Quote Link to comment https://forums.phpfreaks.com/topic/179183-global-variable/ Share on other sites More sharing options...
WolfRage Posted October 27, 2009 Share Posted October 27, 2009 There is no real point, it should be removed, as long as you are assiging the function call to a varibale then that variable will contian the returned value of the function. So $shopconfig from the first function will contain the data you want and there is no need to make it a global. Quote Link to comment https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945367 Share on other sites More sharing options...
jonniejoejonson Posted October 27, 2009 Share Posted October 27, 2009 I think that you have to set it as global becuase otherwise it would not be accessible to the function. regards J Quote Link to comment https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945368 Share on other sites More sharing options...
karthikanov24 Posted October 27, 2009 Author Share Posted October 27, 2009 hi From where the global variable, $shopconfig which is inside the function displayAmount() gets the reference(value)..? Is it from the variable $shopconfig inside the config.php file or from the variable $shopconfig inside the function (which is above the function displayAmount()) getshopconfig() in common.php file...? thanks karthikanov24 Quote Link to comment https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945383 Share on other sites More sharing options...
KevinM1 Posted October 27, 2009 Share Posted October 27, 2009 I think that you have to set it as global becuase otherwise it would not be accessible to the function. regards J Untrue. getShopConfig() returns $shopConfig. Simply pass that returned value into displayAmount() through the function's argument list. Using global is a sign of bad design, and isn't necessary to use. Quote Link to comment https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945395 Share on other sites More sharing options...
DavidAM Posted October 27, 2009 Share Posted October 27, 2009 Variables that are used inside of a function are NOT accessible outside of that function. So the $ShopConfig in getShopConfig() is not being used by any other function. However, the values in this variable are returned by the call and placed in a GLOBAL variable which, in this code, is also named $ShopConfig. These are TWO DIFFERENT variables. The function displayAmount() is referring to the global variable $ShopConfig. In fact, that is what the global $ShopConfig; statement does. If that statement was not inside of the function, the function would NOT be able to reference the global variable. Quote Link to comment https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945396 Share on other sites More sharing options...
KevinM1 Posted October 27, 2009 Share Posted October 27, 2009 Variables that are used inside of a function are NOT accessible outside of that function. Unless they're returned from the function and assigned to a variable outside of the function's scope. So the $ShopConfig in getShopConfig() is not being used by any other function. However, the values in this variable are returned by the call and placed in a GLOBAL variable which, in this code, is also named $ShopConfig. These are TWO DIFFERENT variables. Two different variables, yes, but both contain the same value. The function displayAmount() is referring to the global variable $ShopConfig. In fact, that is what the global $ShopConfig; statement does. If that statement was not inside of the function, the function would NOT be able to reference the global variable. Sure it would. Again, that's what argument lists are for. function displayAmount($shopConfig, $amount) { return $shopConfig['currency'] . number_format($amount); } Same result, but better code. Quote Link to comment https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945415 Share on other sites More sharing options...
karthikanov24 Posted October 27, 2009 Author Share Posted October 27, 2009 thanks to all! Quote Link to comment https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945477 Share on other sites More sharing options...
DavidAM Posted October 27, 2009 Share Posted October 27, 2009 Nightslyr: I did not see your post when I posted my response, so I was not contrdicting you, I was trying to answer the OP's question: From where the global variable, $shopconfig which is inside the function displayAmount() gets the reference(value)..? and thought I should provide as much detail as possible. Variables that are used inside of a function are NOT accessible outside of that function. Unless they're returned from the function and assigned to a variable outside of the function's scope. The variable is NOT accessible outside of the function. The value may be returned but it is not the variable. I admit this may seem to be a nit-picky distinction, but the OP seemed to be a beginner and I thought it would be best if he understood the difference. They are not the same variable; a change to one will not affect the other; in fact, the variable inside the function does not even exist after the return. Two different variables, yes, but both contain the same value Exactly the point I was making. The value was returned to a global variable so it could be used later. Sure it would. Again, that's what argument lists are for. Well, I was answering the OP's question about what the line of code in the second function was referencing. Since the question related back to his original question of what the global statement meant, I was trying to tie the whole thing together. Yes, using global can lead to problems; including difficult to read code, ease of accidentally changing a "global" value by changing a value inside a function, etc. However, I do use them sometimes. I think we all use the "super globals" which are in fact globals. The difference is that their existance is well documented. If globals are consistently named and documented inside of functions and outside, they can be very useful. As a general rule, however, I do prefer to pass these as parameters. Part of the confusion here is beause the variables have the same name. This, too, is bad practice. Use a consistent naming style for variables, varying the style by the scope of the variable. I use different styles for function parameters, internal function variables and global variables (as well as a different style for constants) just to make it easier for me when reading my code. Quote Link to comment https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945741 Share on other sites More sharing options...
KevinM1 Posted October 27, 2009 Share Posted October 27, 2009 Ah, okay, sorry for the misunderstanding. There have been a lot of people who swear by relying on global, so I misread your intent. Quote Link to comment https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945753 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.