homepage Welcome to WebmasterWorld Guest from 54.198.42.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
PHP Dynamic Variables
just isn't making sense!
mechanical messiah




msg:1307646
 11:24 pm on Apr 16, 2003 (gmt 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 :)

 

jatar_k




msg:1307647
 11:27 pm on Apr 16, 2003 (gmt 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.

mechanical messiah




msg:1307648
 11:27 pm on Apr 16, 2003 (gmt 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!

mechanical messiah




msg:1307649
 11:32 pm on Apr 16, 2003 (gmt 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 :)

gethan




msg:1307650
 11:36 pm on Apr 16, 2003 (gmt 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);
...

jatar_k




msg:1307651
 11:42 pm on Apr 16, 2003 (gmt 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.

andreasfriedrich




msg:1307652
 12:00 am on Apr 17, 2003 (gmt 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.

grifter




msg:1307653
 5:23 am on Apr 17, 2003 (gmt 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 ;)

jatar_k




msg:1307654
 2:14 pm on Apr 17, 2003 (gmt 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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved