0

I have the following php code where by a for loop create different variables. Basically what I need is, after run the code below, it should create different variables such as $o1, $o2, $o3 etc. But it is not working based on the way I implemented. Does anyone can help me how to solve this?

for($i = 0; $i <= $totalOa; $i++){
      '$o'.$i = $pieces[$i];  
}
5
  • 1
    You want to make an array or which type of output you want, please add your output which you want. Commented Dec 5, 2019 at 9:09
  • Not an array. It should create some variables Commented Dec 5, 2019 at 9:10
  • 1
    Why do you want to create variables? This is an antipattern. Use associative arrays; this is what they're for. Don't create variables dynamically. Commented Dec 5, 2019 at 9:13
  • @Anu As I understand you can make an array and use it like $o[$i] and use according to so I think it works same as string vars. Commented Dec 5, 2019 at 9:15
  • @Dharman Thank you for that suggestion. But for this particular situation, i don't wish to create array. Commented Dec 6, 2019 at 0:55

2 Answers 2

1

Try this...

for($i = 0; $i <= $totalOa; $i++){
     ${"o" . $i} = $pieces[$i]; 
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

You need to use variable variables.

for($i = 0; $i <= $totalOa; $i++){
    $o{$i} = $pieces[$i];  
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.