why use $$ in this example?

Discussion in 'PHP' started by nicholas_whitworth_dt_com, Aug 31, 2007.

  1. #1
    stupid question,




    WITH:


    function tep_db_close($link = 'db_link') {
    global $$link;

    return mysql_close($$link);
    }




    (ignore the function-ality)

    WHY have (the programmers of oscommerce) done $LINK - like

    global $$link;?

    Surely that means $db_link. And So why not say "global $db_link;"
    It's not even a dynamic variable name - because it's set above (assuming that variables set in a function () parentheses behave the same as normal.
    IF they had said $link = CONSTANT_NAME, and that was settable elsewhere - i could see the reasoning.

    also - this is one of a list of functions - all with the same $link = 'db_link' thing in their ... "header" () parentheses [i don't know what to call it].

    Any ideas?

    nicholas.
     
  2. loibeignacio

    loibeignacio Peon

    Messages:
    13
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    hmm variable variables :)

    kindly hav a look here:

    http://www.php.net/manual/en/language.variables.variable.php
     
    loibeignacio, Aug 31, 2007 IP
  3. nicholas_whitworth_dt_com

    nicholas_whitworth_dt_com Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Cheers... i got that though...

    The answer is that they WERE setting only a DEFAULT value... so $link could equal "shoes" in the function - were "shoes" passed to it - the defualt ('db_link') would be over-rulled.

    Like this:


    function defualt_values_test ($a = 123, $b = 456){
    echo "a = ".$a."<br/>";
    echo "b = ".$b."<br/>";
    echo "<br/>";
    }


    defualt_values_test(); // uses values set in 'header'
    defualt_values_test('overwritten',987654321); // uses these values
    defualt_values_test($non_existant,"var A has to be overwritten."); // you can only use this for the last vars.



    OUTPUTS
    ----------

    a = 123
    b = 456

    a = overwritten
    b = 987654321

    a =
    b = var A has to be overwritten.
     
  4. ssanders82

    ssanders82 Peon

    Messages:
    77
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    ssanders82, Aug 31, 2007 IP