Jump to content


Photo

Is there any reason for using $$


  • Please log in to reply
5 replies to this topic

#1 Razzeal

Razzeal
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 11 July 2006 - 01:12 PM

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

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 11 July 2006 - 01:14 PM

are you sure this is PHP?
Life's too short for arguing.

#3 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 11 July 2006 - 01:15 PM

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.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 July 2006 - 01:18 PM

Yes, it's PHP. Using a double $ indicates that it is a variable variable. See http://w.php.net/man...es.variable.php for more information.

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

Ken

#5 ShogunWarrior

ShogunWarrior
  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 11 July 2006 - 01:39 PM

It comes in handy for stuff like object properties like so:
//*Inside the class*
function setVar($i)
{
  $name = 'id' . strval($i);
  $this->$$name;
}

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

And now you can use $x->id15

It would come in handy parsing data so you could refer to the data by properties like:
$doc->xml->title etc. etc.
<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

#6 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 11 July 2006 - 02:00 PM

Using a double $ indicates that it is a variable variable.


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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users