Jump to content

Recommended Posts

I think this is how it would work.

Say you have a variable called $name.
[code]$name = "john";[/code]

And say you want to assign the NAME of another variable based on $name. I honestly don't know why you'd want to, but you do. :)
You can then do this:

[code]$$name = "male";[/code]

It puts the contents of $name ("john") in place of "$name", so you effectively have:
[code]$john = "male";[/code]

So now you can work with the variable $john.
Link to comment
https://forums.phpfreaks.com/topic/4175--/#findComment-14498
Share on other sites

Variable variables (as they are called) can be very useful.

neylitalo has the basics correct....

$var = 'name';

creates a container so you can vary the varibale used

so echoing $$var would echo out the contents of $name.

It becomes really useful looping through similar variables like so..

say you have lots of variables called:

foo1, foo2, foo3....foo4

now you don't have any array to loop through as they are all independant vars as far as php is concerned

BUT if you do this

$i = 1;

do {
$var = 'foo$i';
echo $$var;
$i++;
} while ($$var);

that would echo out each and every variable fooNUMBER.

Link to comment
https://forums.phpfreaks.com/topic/4175--/#findComment-14503
Share on other sites

so
$john="john";
$john="male";

adding the first douler sign gives john and then john turns into another varable to add $john as male.

i think i go that right thank you.


[!--sizeo:6--][span style=\"font-size:24pt;line-height:100%\"][!--/sizeo--]solved[!--sizec--][/span][!--/sizec--]

correted echo $$john;
Link to comment
https://forums.phpfreaks.com/topic/4175--/#findComment-14504
Share on other sites

no.
[code]
$name = 'john';

$var = 'name';

echo $name; // outputs 'john'
echo $var; // outputs 'name'
echo $$var; // outputs john

$name1 = 'john';
$name2 = 'pete';

$i = 1;
$var = 'name$i';
do {
echo $$var . "<br>";
$i++;
} while ($$var);

// this will output..
john
pete[/code]
Link to comment
https://forums.phpfreaks.com/topic/4175--/#findComment-14508
Share on other sites

Thank you for your time but i dont understand looping yet but thank you.$i = 1;

// assign name 1 and name 2 to john and pete.
$name1 = 'john';
$name2 = 'pete';


//assign $i to a 1
$i=1;

// assign $var to name$i and $i
$var = 'name$i';

//do somethink and brace
do {

//echo $$var with brake
echo $$var . "<br>";
// $i plus 1 more
$i++;

//close brace while $$ is var
} while ($$var);

will print john and pete

try to understand lol
Link to comment
https://forums.phpfreaks.com/topic/4175--/#findComment-14511
Share on other sites

[!--quoteo(post=351941:date=Mar 5 2006, 07:54 PM:name=ToonMariner)--][div class=\'quotetop\']QUOTE(ToonMariner @ Mar 5 2006, 07:54 PM) [snapback]351941[/snapback][/div][div class=\'quotemain\'][!--quotec--] Variable variables (as they are called) can be very useful.

neylitalo has the basics correct....

$var = 'name';

creates a container so you can vary the varibale used

so echoing $$var would echo out the contents of $name.

It becomes really useful looping through similar variables like so..

say you have lots of variables called:

foo1, foo2, foo3....foo4

now you don't have any array to loop through as they are all independant vars as far as php is concerned

BUT if you do this

$i = 1;

do {
$var = 'foo$i';
echo $$var;
$i++;
} while ($$var);

that would echo out each and every variable fooNUMBER.

[/quote]

That's the only thing I can see it coming in handy for. However, there is an easier way. Now, I'm not saying your method is wrong, but I want to point out one little thing.

Why not just create an array? ;) Call it $foo, and have the keys be numbers. And you can do so much more stuff with arrays. foreach loops, sorting, the whole nine yards.

And quite honestly, I don't see ANY situation where it would be easier to have a bunch of variables than a single array with several keys.
Link to comment
https://forums.phpfreaks.com/topic/4175--/#findComment-14541
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.