Difference between $name and $$name?

by 10 replies
14
Please some one explain the difference between $name and $$name?
#programming #$$name #$name #difference
  • Tomos is right, the $$ represents a variable variable. Here's how they work

    $foo = 'bar';
    $name = 'foo';

    echo $name; // foo
    echo $$name; //bar
    echo $foo; //bar

    You first assign the value of a variable, $name, to the name of another variable. When you set $name to a value, it will replace that variablename 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!
    • [1] reply
    • Hi..Thanks for your help...

      The above coding is easily understandable..

      It nice explanations...Thanks a lot.....
  • I'd add the suggestion to not use this style if you can avoid it, it makes the code hard to read without a step through debugger.
    • [1] reply
    • This. Seriously, don't be that guy. PHP is supposedly designed with the "KISS Principle" (Keep It Simple Stupid), but they allow some utterly attrocious things. I truly can't think of a reason you'd ever want to do this that can't be done cleaner and/or simpler in another way.
      • [ 1 ] Thanks
      • [2] replies
  • Banned
    [DELETED]
  • $name is variable where as $$name is reference variable
  • Banned
    [DELETED]
  • $name is a variable while $$name is a reference variable.
  • Banned
    [DELETED]

Next Topics on Trending Feed