RE: [PHP] parsing variables inside a variable?
| From: | Ford, Mike [LSS] | Date: | Fri, 30 Jan 2004 10:11:28 +0000 |
| Subject: | RE: [PHP] parsing variables inside a variable? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-176323@lists.php.net to get a copy of this message | ||
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