NYCPHP Meetup

NYPHP.org

[nycphp-talk] Quick little question - variable interpolation

Adam Fields fields at surgam.net
Thu Oct 17 16:24:20 EDT 2002


On Thu, Oct 17, 2002 at 04:01:52PM -0400, Analysis & Solutions wrote:
> Just to make sure everything is clear to Adam:
> 
> Only strings inside double quotes get evaluated.  Strings inside single
> quotes are taken literally.

Yes, I'm fully aware of that. That was not my question.

> So:
> 
>  $one = 1;
>  $two = "this string = $one and that is it";
>  echo $two;
>  [results:  this string = 1 and that is it]
> 
>  $one = 1;
>  $two = 'this string = $one and that is it';
>  echo $two;
>  [results:  this string = $one and that is it]

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);

-- 
				- Adam

-----
Adam Fields, Managing Partner, fields at surgam.net
Surgam, Inc. is a technology consulting firm with strong background in
delivering scalable and robust enterprise web and IT applications.
http://www.adamfields.com



More information about the talk mailing list