Jump to content

Archived

This topic is now archived and is closed to further replies.

Razzeal

Is there any reason for using $$

Recommended Posts

I've been assigned to improve a piece of code but i've stumbled into a problem.
The code uses a variable called $$id, in the database this number should equal 640, 641, 642 etc
The variable is like this:

$id = "i" + $index (index is a number from 1 to 15)

and then in the query it says:

Where id =". $$id

I need to get the id and I need it to be something like 640 etc, but i don't know what i should do...

can anybody help me???

thx,

Razz

Share this post


Link to post
Share on other sites
are you sure this is PHP?

Share this post


Link to post
Share on other sites
The use of $$ is using a variable variable. Its quite hard to explain why to use them and what they do...and i dont quite understand the use of them in your script either.

Share this post


Link to post
Share on other sites
Yes, it's PHP. Using a double $ indicates that it is a variable variable. See http://w.php.net/manual/en/language.variables.variable.php for more information.

If the index needs to be a different number, use a different mechnism for setting it:
[code]<?php
$some_maximum = 999; // set this to the highest $id+1
for ($id = 640;$id < $some_maximum;$id++) {
//
//  do stuff
//
}?>[/code]

Ken

Share this post


Link to post
Share on other sites
It comes in handy for stuff like object properties like so:
[code]
//*Inside the class*
function setVar($i)
{
  $name = 'id' . strval($i);
  $this->$$name;
}

//Set property
$x->setVar(15);
[/code]

And now you can use [b]$x->id15[/b]

It would come in handy parsing data so you could refer to the data by properties like:
[b]$doc->xml->title[/b] etc. etc.

Share this post


Link to post
Share on other sites
[quote author=kenrbnsn link=topic=100190.msg395097#msg395097 date=1152623923]Using a double $ indicates that it is a variable variable.[/quote]

Heh, didn't knew they were called that... quite a funny name

Share this post


Link to post
Share on other sites

×

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.