0

I would like to create a loop that names the variables based on i so it should be $l_name0, $lname1, etc. This is what I tried to do, however it just looped forever and did not work.

for ($i=0; $i<=2; $i++) {
    $L_NAME.$i='name'.$i;
    $L_AMT.$i='amt'.$i;
    $L_QTY.$i='qty'.$i;
    echo $L_NAME.$i;
}

Any ideas how I can do this?

2
  • 3
    Is there a reason why you need them as variables? It's much easier to do this in an array! Commented Jun 20, 2012 at 19:48
  • 1
    You almost certainly don't want to do what you're trying to do. This is what arrays are for. Commented Jun 20, 2012 at 19:50

1 Answer 1

5

Here is what you did wrong

for ($i=0; $i<=2; $i++) {
    ${'L_NAME'.$i} = 'name'.$i;
    ${'L_AMT'.$i} = 'amt'.$i;
    ${'L_QTY'.$i} = 'qty'.$i;
    echo $L_NAME.$i;
}
Sign up to request clarification or add additional context in comments.

Comments

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.