Simple Variable Syntax Question.

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

    #1

    Simple Variable Syntax Question.

    I am receiving 40 variables from an external source into my php script.
    I need to have for loop cycle through those and assign them to
    variables in the php script. I am unsure of the syntax, but it would be
    something like the following

    for($i=0;$i<40; $i++){
    $phpvar+$i = receivedData+$i
    }


    The data coming in is named as follows data1, data2, data3, ... data40.
    So I need the php variables to be named in the same way. Whats the
    syntax here? Thanks for any help.

  • Janwillem Borleffs

    #2
    Re: Simple Variable Syntax Question.

    blackberryoctop us wrote:
    The data coming in is named as follows data1, data2, data3, ...
    data40. So I need the php variables to be named in the same way.
    Whats the syntax here? Thanks for any help.
    >
    ${"data$i"} = receivedData+$i ;

    But it would make more sense to create an array:

    $data = array();
    for ( ... ){
    $data[] = receivedData+$i ;
    }


    JW


    Comment

    • ZeldorBlat

      #3
      Re: Simple Variable Syntax Question.


      blackberryoctop us wrote:
      I am receiving 40 variables from an external source into my php script.
      I need to have for loop cycle through those and assign them to
      variables in the php script. I am unsure of the syntax, but it would be
      something like the following
      >
      for($i=0;$i<40; $i++){
      $phpvar+$i = receivedData+$i
      }
      >
      >
      The data coming in is named as follows data1, data2, data3, ... data40.
      So I need the php variables to be named in the same way. Whats the
      syntax here? Thanks for any help.
      Data coming in from where? GET? POST? Command line? Cookies? Let's
      suppose it's POST and you have POST variables named data1, data2, ...
      data40. Then it would be something like this:

      for($i = 1; $i <= 40; $i++) {
      $name = "data$i";
      $$name = $_POST[$name];
      }

      Comment

      • Geoff Berrow

        #4
        Re: Simple Variable Syntax Question.

        Message-ID: <1168533804.991 748.11600@i56g2 000hsf.googlegr oups.comfrom
        ZeldorBlat contained the following:
        >The data coming in is named as follows data1, data2, data3, ... data40.
        >So I need the php variables to be named in the same way. Whats the
        >syntax here? Thanks for any help.
        >
        >Data coming in from where? GET? POST? Command line? Cookies? Let's
        >suppose it's POST and you have POST variables named data1, data2, ...
        >data40. Then it would be something like this:
        >
        >for($i = 1; $i <= 40; $i++) {
        $name = "data$i";
        $$name = $_POST[$name];
        >}
        Or you could use extract

        extract($_POST) ;

        Or you could just use the array elements directly

        echo $_POST['data1'];
        --
        Geoff Berrow (put thecat out to email)
        It's only Usenet, no one dies.
        My opinions, not the committee's, mine.
        Simple RFDs http://www.ckdog.co.uk/rfdmaker/

        Comment

        • denis

          #5
          Re: Simple Variable Syntax Question.

          There is a shorter way I think. If we assume those are POST variables, the
          code might look something like

          foreach ($_POST as $name =$value)
          $name = $value;

          "ZeldorBlat " <zeldorblat@gma il.comwrote in message
          news:1168533804 .991748.11600@i 56g2000hsf.goog legroups.com...
          >
          blackberryoctop us wrote:
          >I am receiving 40 variables from an external source into my php script.
          > I need to have for loop cycle through those and assign them to
          >variables in the php script. I am unsure of the syntax, but it would be
          >something like the following
          >>
          >for($i=0;$i<40 ;$i++){
          > $phpvar+$i = receivedData+$i
          >}
          >>
          >>
          >The data coming in is named as follows data1, data2, data3, ... data40.
          >So I need the php variables to be named in the same way. Whats the
          >syntax here? Thanks for any help.
          >
          Data coming in from where? GET? POST? Command line? Cookies? Let's
          suppose it's POST and you have POST variables named data1, data2, ...
          data40. Then it would be something like this:
          >
          for($i = 1; $i <= 40; $i++) {
          $name = "data$i";
          $$name = $_POST[$name];
          }
          >

          Comment

          • blackberryoctopus

            #6
            Re: Simple Variable Syntax Question.


            Geoff Berrow wrote:
            Message-ID: <1168533804.991 748.11600@i56g2 000hsf.googlegr oups.comfrom
            ZeldorBlat contained the following:
            >
            The data coming in is named as follows data1, data2, data3, ... data40.
            So I need the php variables to be named in the same way. Whats the
            syntax here? Thanks for any help.
            Data coming in from where? GET? POST? Command line? Cookies? Let's
            suppose it's POST and you have POST variables named data1, data2, ...
            data40. Then it would be something like this:

            for($i = 1; $i <= 40; $i++) {
            $name = "data$i";
            $$name = $_POST[$name];
            }
            Or you could use extract
            >
            extract($_POST) ;
            >
            Or you could just use the array elements directly
            >
            echo $_POST['data1'];
            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/
            I know that referencing the elements directly would work, but it would
            be cumbersome and in flexible for future revisions. I am trying to
            avoid this; so some sort of for loop to populate an array or multiple
            variables in php seemed like the best way.

            Comment

            • jussist@gmail.com

              #7
              Re: Simple Variable Syntax Question.

              I definately would suggest you to use array, as advised in the first
              post. Dynamic variable -names are usually result of bad design.

              Comment

              • Geoff Berrow

                #8
                Re: Simple Variable Syntax Question.

                Message-ID: <45a6ae8b$1@new s.bihnet.bafrom denis contained the
                following:
                >There is a shorter way I think. If we assume those are POST variables, the
                >code might look something like
                >
                >foreach ($_POST as $name =$value)
                $name = $value;
                Nearly.

                foreach ($_POST as $name =$value){
                $$name = $value;
                }

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

                Comment

                • denis

                  #9
                  Re: Simple Variable Syntax Question.

                  Yep, I saw it as well and hoped someone would correct.

                  Thanks.

                  "Geoff Berrow" <blthecat@ckdog .co.ukwrote in message
                  news:n4ndq210ae 5ssfogfd85m7lhu f9ju42p2q@4ax.c om...
                  Message-ID: <45a6ae8b$1@new s.bihnet.bafrom denis contained the
                  following:
                  >
                  >>There is a shorter way I think. If we assume those are POST variables, the
                  >>code might look something like
                  >>
                  >>foreach ($_POST as $name =$value)
                  > $name = $value;
                  >
                  Nearly.
                  >
                  foreach ($_POST as $name =$value){
                  $$name = $value;
                  }
                  >
                  --
                  Geoff Berrow (put thecat out to email)
                  It's only Usenet, no one dies.
                  My opinions, not the committee's, mine.
                  Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                  Comment

                  Working...