0

I am trying to create a loop that creates variables using the increment/count number:

$names = 4;

for($it=0;$it<$names;$it++){ 
           $NAME{$it}       = $_REQUEST['NAME'.$it.''];
           $SURNAME{$it}    = $_REQUEST['SURNAME'.$it.''];
           $AGE{$it}        = $_REQUEST['AGE'.$it.''];
 }

My issue is that instead of getting $NAME0, $NAME1 etc, I am getting an array (so $NAME[0], $NAME[1] and so on).

How would I use the loop to get all the info in $NAME0, $NAME1, $NAME2, $SURNAME1, $SURNAME2 $SURNAME3, $AGE1, $AGE2, $AGE3?

Thanks :)

7
  • 4
    Why not use an array‽ It's the vastly saner method! Commented Apr 26, 2012 at 3:25
  • 2
    Why don't you want an array? You can iterate over arrays, slice them, and do all kinds of things that you can't do with a bunch of variables with related names. Commented Apr 26, 2012 at 3:25
  • 2
    By all means, you should do this with an array rather than a pile of global variables. Commented Apr 26, 2012 at 3:27
  • Sorry for answering the question, rather than assuming what someone needs. There was absolutely no reason to vote down my answer, but hey, go for it! Commented Apr 26, 2012 at 3:30
  • @CassieCarter: I don't think it works: codepad.org/XUxxGxyL Commented Apr 26, 2012 at 3:32

3 Answers 3

4

You don't want variables with related names; using an array is much simpler. In this case, $NAME0 is the same as $NAME[0], except you can do a lot more things with $NAME[0] than you can with $NAME0. Stick with the array, learn to use it; don't reinvent the wheel.

Sign up to request clarification or add additional context in comments.

Comments

0

Generally speaking, this would be dangerous for super-globals (e.g. $_REQUEST) without serious forethought.

However, this is an excellent use case for the underused PHP function extract().

If you were to use it in such a case, I'd strongly recommend setting the additional parameters with the EXTR_PREFIX_ALL flag.

2 Comments

It's most likely Elliot Bonneville down voting because we offered solutions different than his... Citing we are "giving the OP what they want."
@JT Smith: Hey, don't be hatin'. I didn't downvote either of the other questions, just fyi. :)
-1

I agree with the others about using an array, much better but if that's not what you want, try adding an underscore to the variables... May not be "perfect" but won't create an array:

$names = 4;

for($it=0;$it<$items;$it++){ 
       $NAME_{$it}       = $_REQUEST['NAME'.$it.''];
       $SURNAME_{$it}    = $_REQUEST['SURNAME'.$it.''];
       $AGE_{$it}        = $_REQUEST['AGE'.$it.''];
}

4 Comments

Yes, it's far from perfect. In fact, it's another bad example of giving the OP what they want rather than what they need. This method will generally give them lots of trouble later on as they start to write more complex code.
Elliot, what you've just described is called learning. The OP has asked a question. This is not a bad example, its an option.
@ElliotBonneville I am "giving the OP what they want" because that is what they asked for. I presume they asked for it because that's what they want. It may not be the best solution, as I agree with using it as an array, however, we tell them the best procedure while still answering their questions to fill their particular needs. A person can name variables however they wish.
That is true. However, why would you want to create an artificial learning curve for somebody? Just tell them what they actually need. I'd be grateful it somebody did that for me when I was learning. In fact, I'm still grateful if somebody does that for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.