Jump to content

Is there any reason for using $$


Razzeal

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
Link to comment
https://forums.phpfreaks.com/topic/14289-is-there-any-reason-for-using/
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
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.

Archived

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

×
×
  • 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.