Page 1 of 1

Make a dynamic variable

Posted: Sat Aug 28, 2010 10:40 am
by xionhack
Hello. I want to create some variables dynamically. I have this so far:

Code: Select all

 

for($i = 1; $i <= 4 ; $i++){
	$a = 'member' . $i;
        $$a = array();
}
Does that give me this 4 arrays?:

$member1, $member2, $member3 and $member4? Of course I want some process inside of the loop, but that would depend on the variables. Would that work? thanks!

Re: Make a dynamic variable

Posted: Sat Aug 28, 2010 11:53 pm
by JakeJ
You're correct, that would give you 4 different arrays with the names you mentioned.

Re: Make a dynamic variable

Posted: Sun Aug 29, 2010 2:02 pm
by Jonah Bron
Yes, but dynamic variables are something that can (almost) always be more neatly handled with arrays.

Code: Select all

$members = array();
for($i = 0; $i < 4 ; $i++){
    $members[$i] = array();
}