0

how to get value or same name variable but end with different index

 $item1='100';
 $item2='200';
 $item3='300';
 $item4='400';
 $item5='500';

 for ($i=1;$i<4;$i++){
    echo '--'.$($item.$i); // or echo '--'.$.'item'.$i);
 }

i have variable $item1,2,3,.... i don't know how many item variable i got . i got total no of item variable so i want to go with loop and print all item variable value . i write above code but error occur . any one know how to get each item value ????

4
  • 3
    Why not using Array? Commented Dec 31, 2014 at 11:37
  • in my case array not possible . other wise with array all is very simple Commented Dec 31, 2014 at 11:38
  • 1
    @Affan explain why you can't save values to array indexes, but can save them to random named variables... Commented Dec 31, 2014 at 11:39
  • i don't know why people give negative marking without give any solution . solution is there . i place $($itme) instead of {} . Commented Dec 31, 2014 at 11:43

2 Answers 2

2

You can try like

echo '--'.${"item" . $i};

Refer this.And better you use an array like

$item[1]='100';
$item[2]='200';
$item[3]='300';
$item[4]='400';
$item[5]='500';
Sign up to request clarification or add additional context in comments.

Comments

0

A better approach would be to use an array. But if you need to use dynamic variables, the syntax is:

echo '--'.${$item.$i};

See Variable Variables.

1 Comment

yes i understand but i work with payment method and it provide me only variable and each time different no of variable so i need this . after get this i will create array from this and do my further process . thanks for help @Barmar

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.