On Sat, 2012-09-22 at 13:13 +0200, Maciek Sokolewicz wrote: > On 22-09-2012 12:34, Ashickur Rahman Noor wrote: > > Hi all > > > > I need some help to understand a code. The code is like this > > > > $result = mysql_query($sSQL) or die("err: " . mysql_error().$sSQL); > >> if($row = mysql_fetch_array($result)) > >> { > >> foreach($row as $key =>$value){ $$key=$value;} > >> } > >> > > > > I don't get the code from the foreach loop. > It simply assigns all values from the array to variables, which have the > name of the keys. > > So basically what happens is: > $array = array('a'=>1, 'b'=>2, 'c'=>3); > foreach($array as $key=>$val) { > $$key = $val; > } > > will result in the creation of: > $a = 1; > $b = 2; > $c = 3; > > $$foo means "I want a variable whose name is contained in the variable > $foo". So if $foo has the value 'bar', then you'll actually be saying "I > want a variable whose name is 'bar': $bar". > > So: > $foo = 'bar'; > $bar = 'this works!'; > > echo $$foo; // returns "this works!" > > > Be careful with this though. I'm working on fixing some old code that someone wrote. They used this technique to "update" their code when it got moved to a server where register_globals was turned off. -- Thanks, Ash http://www.ashleysheridan.co.uk