Razzeal Posted July 11, 2006 Share Posted July 11, 2006 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 etcThe variable is like this:$id = "i" + $index (index is a number from 1 to 15)and then in the query it says:Where id =". $$idI 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 More sharing options...
hvle Posted July 11, 2006 Share Posted July 11, 2006 are you sure this is PHP? Link to comment https://forums.phpfreaks.com/topic/14289-is-there-any-reason-for-using/#findComment-56160 Share on other sites More sharing options...
GingerRobot Posted July 11, 2006 Share Posted July 11, 2006 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. Link to comment https://forums.phpfreaks.com/topic/14289-is-there-any-reason-for-using/#findComment-56161 Share on other sites More sharing options...
kenrbnsn Posted July 11, 2006 Share Posted July 11, 2006 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+1for ($id = 640;$id < $some_maximum;$id++) {//// do stuff//}?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/14289-is-there-any-reason-for-using/#findComment-56163 Share on other sites More sharing options...
ShogunWarrior Posted July 11, 2006 Share Posted July 11, 2006 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 https://forums.phpfreaks.com/topic/14289-is-there-any-reason-for-using/#findComment-56177 Share on other sites More sharing options...
Daniel0 Posted July 11, 2006 Share Posted July 11, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/14289-is-there-any-reason-for-using/#findComment-56202 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.