variable declaration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AMT India
    New Member
    • Feb 2007
    • 64

    variable declaration

    What is the difference between $a and $$a in php? $a = 10 and $$a = 10 are working as same. But $$$a is not working.
  • henryrhenryr
    New Member
    • Jun 2007
    • 103

    #2
    Originally posted by AMT India
    What is the difference between $a and $$a in php? $a = 10 and $$a = 10 are working as same. But $$$a is not working.

    Try the manual: http://www.php.net/manual/en/language.variab les.php

    Or http://php.net/manual/en/language.variab les.variable.ph p

    I think it would work if you used ${${$a}} but not sure.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, AMT India.

      Originally posted by AMT India
      What is the difference between $a and $$a in php?
      $$a is a 'variable variable'. PHP evaluates the rightmost part of the expression ($a) and then uses that value with the left part.

      So for example:
      [code=php]
      $name = 'red';

      $red = 50;
      $green = 75;
      $blue = 100;

      echo $$name; // evaluates to ${'red'} -> $red -> 50
      [/code]

      Originally posted by AMT India
      $a = 10 and $$a = 10 are working as same. But $$$a is not working.
      If you get to the point where you're using variable variable variables, you might want to rethink your application design.

      Comment

      Working...