Re: parsing variables inside a variable?

From: Date: Fri, 30 Jan 2004 12:35:28 +0000
Subject: Re: parsing variables inside a variable?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-176332@lists.php.net to get a copy of this message
Great - thanks v. much Mike. I don't know why there was no mention of eval() in the section of the manual on Variable Parsing. James -- -------------------------------------------------------- www.jholt.co.uk : affordable business website solutions www.htpshareware.com : software for the disorganized -------------------------------------------------------- "You don't needs eyes to see, you need vision" - Maxi Jazz "Mike Ford" <M.Ford@leedsmet.ac.uk> wrote in message news:841D90E489448A4F804E1D1B95768BF7D46343@lis-exchange3.lmu.ac.uk... > On 29 January 2004 14:56, jimbo wrote: > > > Hi, > > > > I have a query regarding variable parsing. > > > > I have some text in a MySQL database in which I have included some > > variable names. Eg (but w/o quotes): "thankyou $name for registering > > on our web site". > > > > I query and use mysql_fetch_array to get the data into an associative > > array. I then build a string and output it like this: > > echo "blah blah ".$row["thecolumn"]." blah blah"; > > > > However, the output is simply: "blah blah thankyou $name for > > registering on our web site blah blah" - i.e. $name does not get > > parsed. I have tried wrapping $name with curly brackets in the > > database but that doesn't help and I have also tried using both > > addslashes and removeslashes on $row["thecolumn"] and I have also > > tried this: echo "blah blah $row['thecolumn'] blah blah" - again both > > with and without curly brackets. > > > > Nothing seems to work. Is what I am trying to do possible? > > Yes, but you need to force PHP to re-evaluate the string you retrieve from > the database -- normally, PHP just uses values retrieved at runtime as-is > and doesn't do any special interpretation on them. > > The function you need to do this is eval() (http://www.php.net/eval), and > one possible way of using it to achieve your desired result is (off the top > of my head, untested): > > eval('echo "blah {$row[\'thecolumn\']} blah";'); > > Cheers! > > Mike > > --------------------------------------------------------------------- > Mike Ford, Electronic Information Services Adviser, > Learning Support Services, Learning & Information Services, > JG125, James Graham Building, Leeds Metropolitan University, > Beckett Park, LEEDS, LS6 3QS, United Kingdom > Email: m.ford@leedsmet.ac.uk > Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211

« previous php.general (#176332) next »