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
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
Link to comment
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.
Link to comment
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.