Make a dynamic variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Make a dynamic variable

Post 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!
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Make a dynamic variable

Post by JakeJ »

You're correct, that would give you 4 different arrays with the names you mentioned.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Make a dynamic variable

Post 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();
}
Post Reply