1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

What is the Difference between $var and $$var?

Discussion in 'PHP' started by swiminsoda, Aug 24, 2011.

  1. #1
    Please some one explain the difference between $var and $$var
     
    swiminsoda, Aug 24, 2011 IP
  2. insert

    insert Peon

    Messages:
    148
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $var is just a variable with name var, $$var takes string from variable $var and finds variable of that name. Example will be better:

    $var = 'fruit';
    $$var = 5;
    echo $fruit; // echoes 5

    Is that clear?
     
    insert, Aug 24, 2011 IP
  3. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #3
    $$var uses the value of the variable whose name is the value of $var. It allows you to have a "variable variable" - the program can create the variable name the same way it can create any other string. That's helpful in situations like $item1, $item2, etc. The program can set $var equal to "item" plus a number, then use the value in that variable by referencing $$var.
     
    Rukbat, Aug 24, 2011 IP
  4. thetechtalk

    thetechtalk Active Member

    Messages:
    233
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #4
    Say you have this.

    <?php
    $var = "chocolate";
    $chocolate = "is yummy";

    echo $$var;

    ?>

    It will echo "is yummy"
     
    thetechtalk, Aug 24, 2011 IP
  5. Junioreality

    Junioreality Active Member

    Messages:
    158
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #5
    $$ represents a variable variable. Here's how they work

    $foo = 'bar';
    $var = 'foo';

    echo $var; // foo
    echo $$var; //bar
    echo $foo; //bar

    You first assign the value of a variable, $var, to the name of another variable. When you set $var to a value, it will replace that variable name with the value of the variable you provide it.

    $test = 'asdf';
    $$test = 'I am changing asdf\'s value!';
    echo $$test; // I am changing asdf's value!
    echo $test; // asdf
    echo $asdf; // I am changing asdf's value!
     
    Junioreality, Aug 24, 2011 IP
  6. swiminsoda

    swiminsoda Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    Thanks for your help. The above coding is easily understandable. It nice explanations.

    Thanks.
     
    swiminsoda, Aug 24, 2011 IP
  7. [GotHost] James

    [GotHost] James Guest

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    It's pretty handy, you can even have variable-variable-variables like so: $$$var, if you wanna be crazy, that is.
     
    [GotHost] James, Aug 24, 2011 IP
  8. swiminsoda

    swiminsoda Member

    Messages:
    43
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #8
    finally at the end, It will be confusing and we will not be sure for what we created such a variable....$$$$var :p Pretty insane.
    I Understand it in code wise, But, can some one give me some practical use. In what scenarios we will be using such variables??
     
    swiminsoda, Aug 24, 2011 IP
  9. kanikakaminial

    kanikakaminial Peon

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    $var is just a variable with name var, $$var takes string from variable $var and finds variable of that name. Example will be better:

    $var = 'fruit';
    $$var = 5;
    echo $fruit; // echoes 5

    this example is best
     
    kanikakaminial, Aug 25, 2011 IP
  10. insert

    insert Peon

    Messages:
    148
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    It is used when you need flexibile variable naming... In this case, it is not used much. More used is version with magic properties in objects...
     
    insert, Aug 25, 2011 IP
  11. Junioreality

    Junioreality Active Member

    Messages:
    158
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #11
    Junioreality, Aug 25, 2011 IP
  12. insert

    insert Peon

    Messages:
    148
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    insert, Aug 25, 2011 IP