Welcome to WebmasterWorld Guest from 54.196.232.162

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Dynamic Variables

just isn't making sense!

     
11:24 pm on Apr 16, 2003 (gmt 0)

New User

10+ Year Member

joined:Mar 22, 2003
posts:30
votes: 0



$user ="bob";
$holder="user";
print $$holder;

is the same as:

$user ="bob";
print $user;

so why in example one would you have to use $$holder? why can't $holder work?

also, what is the point in writing it in the way that example on is written? an example for that would be helpful.

thanks guys, sorry i'm a n00b :)

11:27 pm on Apr 16, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


I use Variable Variables [php.net] quite a bit.

I usually use the ${$a} style. I use them for creating dynamic numbers of arrays etc.

11:27 pm on Apr 16, 2003 (gmt 0)

New User

10+ Year Member

joined:Mar 22, 2003
posts:30
votes: 0


You can use a string constant to define a dynamic variable instead of a variable. To do so, you must wrap the string you want to use for the variable name in braces:

${"user"} = "bob";

How is that useful? If someone can give me an easy to understand answer i'd be very appreciative!
11:32 pm on Apr 16, 2003 (gmt 0)

New User

10+ Year Member

joined:Mar 22, 2003
posts:30
votes: 0


I read the suggested link, i guess i'm stupid :(

i still can't understand how it's helpful, as all you're doing is calling the original "definition" of a, right?

if there's any way you could put a tiny piece of code for me to see how it might be used that'd be helpful :)

11:36 pm on Apr 16, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 8, 2001
posts:926
votes: 0


I use them often as well... here's something cut straight out of the very file I'm editing right now.

FYI - javascript_string is a little function that rewrites a string in to be used in javascript functions later - makes quotes and html code safe.


$array = array('time','comment','name','venname','start','name2');
foreach ($array as $val) {
javascript_string($$val);
}

A lot shorter than the alternative syntax...

javascript_string($time);
...

11:42 pm on Apr 16, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


while (is_array(${$teams[$tc]}[$counter]))
{
?>
<TR>
<TD VALIGN=TOP ALIGN=LEFT><?= ${$teams[$tc]}[$counter][1]?></TD>
<TD VALIGN=TOP ALIGN=CENTER><?= ${$teams[$tc]}[$counter][3]?></TD>
<TD VALIGN=TOP ALIGN=CENTER><INPUT TYPE =TEXT NAME="assists<?= $counter?>" VALUE="<?= ${$teams[$tc]}[$counter][4]?>" size=6></TD>
<TD VALIGN=TOP ALIGN=CENTER><INPUT TYPE =TEXT NAME="pim<?= $counter?>" VALUE="<?= ${$teams[$tc]}[$counter][5]?>" size=6></TD>
<TD VALIGN=TOP ALIGN=CENTER><?= ${$teams[$tc]}[$counter][6]?></TD>
<TD VALIGN=TOP ALIGN=CENTER><INPUT TYPE =TEXT NAME="g_wins<?= $counter?>" VALUE="<?= ${$teams[$tc]}[$counter][7]?>" size=6></TD>
<TD VALIGN=TOP ALIGN=CENTER><INPUT TYPE =TEXT NAME="so<?= $counter?>" VALUE="<?= ${$teams[$tc]}[$counter][8]?>" size=6></TD>
<TD VALIGN=TOP ALIGN=CENTER><?= ${$teams[$tc]}[$counter][9]?></TD>
<TD VALIGN=TOP ALIGN=CENTER><?= ${$teams[$tc]}[$counter][10]?></TD>
</TR>
<?
$counter++;
}
echo "<TR><TD VALIGN=TOP ALIGN=CENTER COLSPAN=9><INPUT TYPE=SUBMIT VALUE=\"Update $teams[$tc]'s Team\"></TD></TR>\n</TABLE></FORM>\n";

this is a case from a switch that I use (it's a little lazy ;)) this one case handles the display of content based on multiple vars but this little guy with the variable vars does it all for me. Makes code confusing sometimes but it works really well. In this particular case if I didn't use them the code would be, at least, 5 times the size.

I use a var to name the array(s) of "stuff" attributed to it. I can then use that single var to connect all the data together. Before this above case is executed there is a lot of calculation done so I create the arrays as I go and use the "parent" var to display.

Variable variables are useful for repetition.

<added>

i guess i'm stupid

sorry to burst your bubble but you're not :)

I had one heck of a time keeping them straight the first time I used them. With out a specific situation where they are needed it is hard to wrap your head around them.

12:00 am on Apr 17, 2003 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:July 22, 2002
posts:1782
votes: 0


YouŽd use variable variable in PHP [php.net] in situations where you would use pointers or references in C and Perl [perl.com] respectively.
5:23 am on Apr 17, 2003 (gmt 0)

Junior Member

10+ Year Member

joined:Jan 7, 2003
posts:106
votes: 0


backing up a little...

$user ="bob";
$holder="user";

print $holder;

output is "user"

print $$holder;

output is "bob"

If you don't understand it now, don't fret. Understanding comes best when you're pressed for a solution ;)

2:14 pm on Apr 17, 2003 (gmt 0)

Administrator

WebmasterWorld Administrator jatar_k is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 24, 2001
posts:15755
votes: 0


YouŽd use variable variable in PHP in situations where you would use pointers or references in C and Perl respectively.

really? I still use references but I find references never quite fit when I would use variable vars. Interesting thought though.

 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members