I want to make 10 variables with name as answer-1,answer-2,answer-3 and so on. Can i use for loop in such a manner to create variables, if not then how can i do it?
<?php
for(i=1;i<=10;i++){
$answer_+i=new array();
}
?>
I want to make 10 variables with name as answer-1,answer-2,answer-3 and so on. Can i use for loop in such a manner to create variables, if not then how can i do it?
<?php
for(i=1;i<=10;i++){
$answer_+i=new array();
}
?>
Your PHP syntax is all wrong (Yes you missed some $
s and added extra new
). You can create them using following syntax. Its called variable variables
for($i=1;$i<=10;$i++){
${"answer_$i"} = array();
}
But I suggest you use array for this. Array is more convenient.
for($i=1;$i<=10;$i++){
$answer[$i] = array();
}
Here your $answer_1
will be $answer[1]
. Best is to use no explicit index
for($i=1;$i<=10;$i++){
$answer[] = array();
}
Now $answer_1
will be $answer[0]
. You can loop over it by for
, foreach
, can use a lot of array functions.