Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/179183-global-variable/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945367
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945383
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945395
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945396
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945415
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/179183-global-variable/#findComment-945741
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.