Jump to content


Photo

$$ << what this mean


  • Please log in to reply
7 replies to this topic

#1 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 06 March 2006 - 12:10 AM

Can someone exspaian what the double varable varables doller sign for, and how to use it, An example would be grate cheers.

examples

//assign a varable is $john='house';

//echo varable is echo $john;

// how to use this double varable method please thank you.
$$whatever


Advance thank you
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#2 Sasuun

Sasuun
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 06 March 2006 - 12:36 AM

I don't think $$ means anything, or that it would really matter if it did. all a variable does is store data, if there is a variable defined with $$ it would function the same as one defined with only one $

most likely, however, while $john is the var john, $$variable is the variable $variable.

#3 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 06 March 2006 - 12:40 AM

I think this is how it would work.

Say you have a variable called $name.
$name = "john";

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:

$$name = "male";

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

So now you can work with the variable $john.

http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#4 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 06 March 2006 - 12:54 AM

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.


follow me on twitter @PHPsycho

#5 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 06 March 2006 - 12:57 AM

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;
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#6 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 06 March 2006 - 01:05 AM

no.
$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

follow me on twitter @PHPsycho

#7 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 06 March 2006 - 01:11 AM

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
Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#8 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 06 March 2006 - 03:11 AM

[!--quoteo(post=351941:date=Mar 5 2006, 07:54 PM:name=ToonMariner)--][div class=\'quotetop\']QUOTE(ToonMariner @ Mar 5 2006, 07:54 PM) View Post[/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.

http://nealylitalo.net - My personal website, and home of The Netizen's Journal.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users