Difference between $name and $$name?

10 replies
Please some one explain the difference between $name and $$name?
#$$name #$name #difference
  • {{ DiscussionBoard.errors[4424048].message }}
  • Profile picture of the author Workman
    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!
    {{ DiscussionBoard.errors[4432364].message }}
    • Profile picture of the author manimoor
      Hi..Thanks for your help...

      The above coding is easily understandable..

      It nice explanations...Thanks a lot.....
      {{ DiscussionBoard.errors[4478311].message }}
  • Profile picture of the author ussher
    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.
    Signature

    "Jamroom is a Profile Centric CMS system suitable as a development framework for building entire communities. Highly modular in concept. Suitable for enterprise level development teams or solo freelancers."

    - jamroom.net
    Download Jamroom free: Download
    {{ DiscussionBoard.errors[4482752].message }}
    • Profile picture of the author Defrag
      Originally Posted by ussher View Post

      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.
      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.
      {{ DiscussionBoard.errors[4483240].message }}
      • Profile picture of the author powerspike
        Originally Posted by Defrag View Post

        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.
        I have to completely agree with this! The absolute worse thing you can do is use it with user input (ie form submit) and you have a complete disaster on your hands.

        Never trust user data, and using $$ under $ is one way to ensure you lose your hair over night.
        {{ DiscussionBoard.errors[4483921].message }}
      • Profile picture of the author Workman
        Originally Posted by Defrag View Post

        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.
        I can think of a couple of places you could use it, but the only one I feel innocent in getting away with would be in a debugging tool of some sort.


        Originally Posted by Sarten View Post

        $ name is variable where as $$ name is reference variable
        Nope. &$name is a variable passed (or accepted in a function call) as a reference.
        [ PHP: References Explained ]
        {{ DiscussionBoard.errors[4541717].message }}
  • Profile picture of the author windso0
    $name is variable where as $$name is reference variable
    {{ DiscussionBoard.errors[4540008].message }}
  • Profile picture of the author Bradpitt
    $name is variable
    And $$name is reference variable in php
    {{ DiscussionBoard.errors[4546933].message }}
  • Profile picture of the author mayurivirani
    $name is a variable while $$name is a reference variable.
    {{ DiscussionBoard.errors[4548308].message }}

Trending Topics