Dynamic Variable Name $$var

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Angelos

    Dynamic Variable Name $$var

    Hello,

    I need to dynamically specify the name of a variable. I just read that
    $varA = "Cat";
    echo $$varA;

    OUTPUT: Cat

    What I try to establish is somehow add a bit of text on my dynamic
    variable name.
    so:

    $varA = "Cat";
    $$varA.'Food' <------ I am not sure what should be the syntax in order
    the variable name to be $CatFood

    The reason I want to do that is because I take the first part of the
    variable name from a Database.

    Thanks.
  • Angelos

    #2
    Re: Dynamic Variable Name $$var

    Angelos wrote:[color=blue]
    > Hello,
    >
    > I need to dynamically specify the name of a variable. I just read that
    > $varA = "Cat";
    > echo $$varA;
    >
    > OUTPUT: Cat[/color]
    SOrry That was wrong example;
    THats a correct one;

    $varA = "dog";
    $$varA = "jack";
    echo "Dog name is: ".$dog;
    OUTPUT: Dog name is: jack[color=blue]
    >
    > What I try to establish is somehow add a bit of text on my dynamic
    > variable name.
    > so:
    >
    > $varA = "Cat";
    > $$varA.'Food' <------ I am not sure what should be the syntax in order
    > the variable name to be $CatFood
    >
    > The reason I want to do that is because I take the first part of the
    > variable name from a Database.
    >
    > Thanks.[/color]

    Comment

    • Angelos

      #3
      Re: Dynamic Variable Name $$var

      I worked it out I think:

      $a = "a";
      $b = "b";
      $varA = $a.$b;

      $$varA = "jack";
      echo "Dog name is: ".$ab;

      Comment

      • Kimmo Laine

        #4
        Re: Dynamic Variable Name $$var

        "Angelos" <angelos@redcat media.net> wrote in message
        news:dv68u4$ba4 $1@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...[color=blue]
        >I worked it out I think:
        >
        > $a = "a";
        > $b = "b";
        > $varA = $a.$b;
        >
        > $$varA = "jack";
        > echo "Dog name is: ".$ab;[/color]

        You can also use the curly braces so you don't need to always assign a new
        variable for that. Example:

        $alpha = 'a';
        $beta = 'b';
        ${$alpha.$beta} = 'all your phps are belong to us';
        echo $ab;

        --
        "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
        spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)


        Comment

        • Ridge Burner

          #5
          Re: Dynamic Variable Name $$var

          > ${$alpha.$beta} = 'all your phps are belong to us';

          Now THAT is funny!


          Comment

          • Angelos

            #6
            Re: Dynamic Variable Name $$var

            Kimmo Laine wrote:[color=blue]
            > "Angelos" <angelos@redcat media.net> wrote in message
            > news:dv68u4$ba4 $1@nwrdmz01.dmz .ncs.ea.ibs-infra.bt.com...
            >[color=green]
            >>I worked it out I think:
            >>
            >>$a = "a";
            >>$b = "b";
            >>$varA = $a.$b;
            >>
            >>$$varA = "jack";
            >>echo "Dog name is: ".$ab;[/color]
            >
            >
            > You can also use the curly braces so you don't need to always assign a new
            > variable for that. Example:
            >
            > $alpha = 'a';
            > $beta = 'b';
            > ${$alpha.$beta} = 'all your phps are belong to us';
            > echo $ab;
            >[/color]
            OHhh thats Great !!!
            Thanks .... Much better way of doing the same thing. ;)

            Comment

            Working...