SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: global variable

  1. #1
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    global variable

    hi
    In the config file of my application,i have the following code,
    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:

    Code:
    <?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

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

  3. #3
    SitePoint Member
    Join Date
    Sep 2009
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi
    From where the global $shopconfig variable gets the reference(value)...
    Is it from $shopconfig variable in getshopconfig() in common.php file OR
    $shopconfig variable in config.php file...?
    please could you explain me clearly...

    thanks
    karthikanov24

  4. #4
    SitePoint Zealot Servyces's Avatar
    Join Date
    Jun 2007
    Location
    The Netherlands
    Posts
    112
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by karthikanov24 View Post
    Is it from $shopconfig variable in getshopconfig() in common.php file OR
    $shopconfig variable in config.php file...?
    please could you explain me clearly...
    The function getShopConfig returns a value with the internal variable name $shopconfig, you could save this output to any wished variable name, which in this case is $shopconfig aswell, but could be anything. This would also work for example:

    PHP Code:
    $testvar getShopConfig(); 
    This would return the output of the getShopConfig function to the variable $testvar. Hope it makes sense.
    Servyces.com
    Where itís all about you.
    Your partner in online solutions.
    Visit our website at http://www.servyces.com/


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •