Thanks Justin I was aware of that method but wanted to avoid it if possible, however another person explained to me that eval() can be used to force PHP to evaluate (i.e. parse) the variables, just thought I'd let you know for your future reference.
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
"Justin Patrin" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED]
Jimbo wrote:
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";
What you need to understand is that the string parsing for variables only happens when the string is actually in your script. When you dynamically create a string (or get it from a DB) it's just a string of characters in memory and is *not* parsed.
To do something like this, you would have to use one of a few things. The first would be to use some kind of search and replace to replace those variables with what you really want.
$text = str_replace('$name', $name, $text);
That's fairly simple and could even be done for multiple variables.
foreach(array('name', 'price') as $varName) { //yes, the $$ is correct $text = str_replace('$'.$varName, $$varName, $text); }
You could also use a regular expression if you *really* wanted to, but what's above is easier.
-- paperCrane <Justin Patrin>
Well, I would have mentioned eval(), but you have to put PHP code in there instead of just strings. For example, "Hello $name." would have to become.
'return "Hello $name."';
or
'return \'Hello \'.$name.\'.\'';
If you eval a string with "Hello $name." in it, you will get a parse error. For readability, I would think that using str_replace to replace specific variables would be better.
Actually, it would be even better if you used a templating system to do this kind of thing, such as Smarty. That way you don't have to worry about variable replacement, but your strings can still be easily edited.
-- paperCrane <Justin Patrin>
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php