NYCPHP Meetup

NYPHP.org

[nycphp-talk] Quick little question - variable interpolation

David Sklar sklar at sklar.com
Thu Oct 17 17:25:35 EDT 2002


> That is also not my question. I don't want to know how to assign a
> string such that variable interpolation happens. I want to know how to
> take an existing string and perform variable interpolation on it. I
> posted my solution already, but I'm still wondering if there's a
> better way.
> 
> For completeness sake, here it is again (this does what I want, given
> an existing string):
> 
> $i = 0;
> foreach (explode(' ', $test) as $word) {
>   if (substr($word, 0, 1) == '$') {
>     $newstring[$i] = eval ("return $word;");
>   } else {
>     $newstring[$i] = $word;
>   }
>   $i++;
> }
> print join(' ', $newstring);

Why explode and rejoin instead of just one eval?

$foo = eval("return '$foo';");

You might need to addslashes() on $foo first:

$safe = addslashes($foo);
$foo  = eval("return '$safe';");

-dave




More information about the talk mailing list