Dereferencing variable name

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

    Dereferencing variable name

    Maybe someone can help me out here:

    $name1 = "Owen";
    $name2 = "Funkhouser ";

    for ($i = 1; $i < 3; $i++ ) {
    echo "<!-- name$i = $name$i -->\n";
    }

    I'd like to see:

    <!-- name1 = Owen -->
    <!-- name2 = Funkhouser -->

    Instead, I get:

    <!-- name1 = 1 -->
    <!-- name2 = 2 -->

    What am I doing wrong?
  • Phlarmf

    #2
    Re: Dereferencing variable name


    "Owen Funkhouser" <funk@highwaay. net> wrote in message
    news:3F710B3C.3 3A56CC3@highwaa y.net...[color=blue]
    > Maybe someone can help me out here:
    >
    > $name1 = "Owen";
    > $name2 = "Funkhouser ";
    >
    > for ($i = 1; $i < 3; $i++ ) {
    > echo "<!-- name$i = $name$i -->\n";
    > }
    >
    > I'd like to see:
    >
    > <!-- name1 = Owen -->
    > <!-- name2 = Funkhouser -->
    >
    > Instead, I get:
    >
    > <!-- name1 = 1 -->
    > <!-- name2 = 2 -->
    >
    > What am I doing wrong?[/color]


    Dynamic variable names are done with the double dollar sign. Try this:

    $name1 = "Owen";
    $name2 = "Funkhouser ";

    for ($i = 1; $i < 3; $i++ ) {
    $varName = "name{$i}";
    echo "<!-- $varName = $$varName -->\n";
    }


    Comment

    • Owen Funkhouser

      #3
      Re: Dereferencing variable name

      Phlarmf wrote:[color=blue]
      >
      > "Owen Funkhouser" <funk@highwaay. net> wrote in message
      > news:3F710B3C.3 3A56CC3@highwaa y.net...[color=green]
      > > Maybe someone can help me out here:
      > >
      > > $name1 = "Owen";
      > > $name2 = "Funkhouser ";
      > >
      > > for ($i = 1; $i < 3; $i++ ) {
      > > echo "<!-- name$i = $name$i -->\n";
      > > }
      > >
      > > I'd like to see:
      > >
      > > <!-- name1 = Owen -->
      > > <!-- name2 = Funkhouser -->
      > >
      > > Instead, I get:
      > >
      > > <!-- name1 = 1 -->
      > > <!-- name2 = 2 -->
      > >
      > > What am I doing wrong?[/color]
      >
      > Dynamic variable names are done with the double dollar sign. Try this:
      >
      > $name1 = "Owen";
      > $name2 = "Funkhouser ";
      >
      > for ($i = 1; $i < 3; $i++ ) {
      > $varName = "name{$i}";
      > echo "<!-- $varName = $$varName -->\n";
      > }[/color]

      I really want to avoid having another variable around. But you got me going in the right
      direction. The following seems to work well:

      for ($i = 1; $i < 3; $i++ ) {
      echo "<!-- name$i = ${"name$i"} -->\n";
      }

      Comment

      • Jochen Daum

        #4
        Re: Dereferencing variable name

        Hi Owen![color=blue]
        >I really want to avoid having another variable around. But you got me going in the right
        >direction. The following seems to work well:
        >
        >for ($i = 1; $i < 3; $i++ ) {
        > echo "<!-- name$i = ${"name$i"} -->\n";
        >}[/color]

        If you want to avoid unnecessary variables you might want to look into
        arrays. See www.php.net

        HTH,Jochen

        --
        Jochen Daum - CANS Ltd.
        PHP DB Edit Toolkit -- PHP scripts for building
        database editing interfaces.
        Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

        Comment

        • Owen Funkhouser

          #5
          Re: Dereferencing variable name

          Jochen Daum wrote:[color=blue]
          >
          > Hi Owen![color=green]
          > >I really want to avoid having another variable around. But you got me going in the right
          > >direction. The following seems to work well:
          > >
          > >for ($i = 1; $i < 3; $i++ ) {
          > > echo "<!-- name$i = ${"name$i"} -->\n";
          > >}[/color]
          >
          > If you want to avoid unnecessary variables you might want to look into
          > arrays. See www.php.net[/color]

          The data actually ends up in an array; I elided that information. In a nutshell, I'm
          converting input from a form's POST into an array.

          Comment

          • Zurab Davitiani

            #6
            Re: Dereferencing variable name

            Owen Funkhouser wrote on Wednesday 24 September 2003 09:29:
            [color=blue]
            > The data actually ends up in an array; I elided that information. In a
            > nutshell, I'm converting input from a form's POST into an array.[/color]

            If you have control over the form being submitted, use an array when
            assigning form element names, such as:

            <input type="text" name="arr[0]" />
            <input type="text" name="arr[1]" />
            etc.

            After the form is submitted, the resulting variable $_POST["arr"] will be an
            array also.

            --
            Business Web Solutions
            ActiveLink, LLC

            Comment

            • Owen Funkhouser

              #7
              Re: Dereferencing variable name

              Zurab Davitiani wrote:[color=blue]
              >
              > Owen Funkhouser wrote on Wednesday 24 September 2003 09:29:
              >[color=green]
              > > The data actually ends up in an array; I elided that information. In a
              > > nutshell, I'm converting input from a form's POST into an array.[/color]
              >
              > If you have control over the form being submitted, use an array when
              > assigning form element names, such as:
              >
              > <input type="text" name="arr[0]" />
              > <input type="text" name="arr[1]" />
              > etc.
              >
              > After the form is submitted, the resulting variable $_POST["arr"] will be an
              > array also.[/color]

              Thank you! This will remove many large chunks of code and reduce the size of the SDD
              greatly.

              Comment

              • Geoff Berrow

                #8
                Re: Dereferencing variable name

                I noticed that Message-ID:
                <skmcb.9176$GO4 .8301@newssvr25 .news.prodigy.c om> from Zurab Davitiani
                contained the following:
                [color=blue]
                ><input type="text" name="arr[0]" />
                ><input type="text" name="arr[1]" />
                >etc.
                >
                >After the form is submitted, the resulting variable $_POST["arr"] will be an
                >array also.[/color]

                So the values will be in $_POST["arr[0]"],$_POST["arr[1]"] and so on?

                --
                Geoff Berrow
                It's only Usenet, no one dies.
                My opinions, not the committee's, mine.
                Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                Comment

                • Pedro

                  #9
                  Re: Dereferencing variable name

                  On Thu, 25 Sep 2003 00:09:52 +0100, Geoff Berrow wrote:[color=blue]
                  > So the values will be in $_POST["arr[0]"],$_POST["arr[1]"] and so on?[/color]

                  Not quite, but very near :-)

                  The values will be in $_POST['arr'][0], $_POST['arr'][1], etc


                  and you can treat the array with the foreach construct:

                  <?php
                  echo '<table>';
                  foreach ($_POST['arr'] as $k=>$v) {
                  echo '<tr><td>', $k, '</td><td>', $v, '</td></tr>';
                  }
                  echo '</table>';
                  ?>

                  --
                  I have a spam filter working.
                  To mail me include "urkxvq" (with or without the quotes)
                  in the subject line, or your mail will be ruthlessly discarded.

                  Comment

                  Working...