Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I would like to declare a string containing a variable name in it, and then have php be able to identify that variable from within the string, like a javascript eval. Examples:

This code works, because the value of $value is what ends up in the string, not "$value".

$value="some value";
$str="the value is $value";
echo $str;
//the value is some value

But i would like this functionality:

$str2="the new value is $newValue";
$newValue="something new";
echo $str2;
//the new value is something new

Of course this does not work in php because $newValue did not exist when $str2 was assigned. Is there anyway for php to do this, to be able to get a variable name from a string, then fetch the value of that variable? It's sort of like a javascript eval I guess. Thanks for reading!

usage: i am constructing html blocks inside of a foreach loop. it loops thru an array, so the information inside of the loop is the key and value of each array element. I am writing a method for a form object that lets the user specify elements of an input block. some of those elements would like access to the key, and some would like access to the value. so for example

foreach($cars as $key=>$car)
{
echo "<input id='text$key' type='text' value='".$car["model"]."'><br>
\n<input value='choice$key' type='button' value='".$car["make"]."'>
<br><br>\n\n";
}

would yield something like

<input id="text0" type="text" value="Malibu"><br>
<input id="choice0" type="button" value="Chevy"><br><br>

<input id="text1" type="text" value="Escort"><br>
<input id="choice1" type="button" value="Ford"><br><br>

I want this to be generated on the fly, so say I have some method called makeCarForm, which takes a string as input, so lets say

function makeCarForm($formString)
{
foreach($cars as $key=>$car)
{
echo $formString;
}
}

and then my string

$theFormString="<input id='text$key' type='text' value='".$car["model"]."'><br>
\n<input value='choice$key' type='button' value='".$car["make"]."'>
<br><br>\n\n";

so I could call the function like

makeCarForm($theFormString);

and it would yield the same HTML output as above. Does this make sense? Am I going about this the wrong way?

share|improve this question
 
Isn't a string reassembling easier? sprintf() –  HAL9000 Jul 5 at 2:16
 
What's the use case? And no, if you have a string result from a interpolation expression, you can't get the previous state, or variable name. Use a printf-style placeholder scheme instead. Or substring search, or whatever you're asking. –  mario Jul 5 at 2:20
 
I have added a use case. –  chiliNUT Jul 5 at 3:04
 
OK I can just do a string replace inside the loop str_replace("$key",$key,$myString), etc. for each value –  chiliNUT Jul 5 at 3:20
add comment

2 Answers

up vote 1 down vote accepted

it is advised that you should not use eval. In the past i have used eval only for a situation where i had to build dynamic boolean logic. if you must use it then you can follow the instructions below.

$string = '$val = "FooBar"; $out = "This is my string $val";'; //when a variable is placed inside single quotes it will not expand.
eval($string);
echo $out;

Once again use of eval is not advised. This is purely for educational purpose.

share|improve this answer
 
This does what I needed, but I appreciate that eval is not the safest method to use. –  chiliNUT Jul 5 at 2:33
add comment

As far as declaration of variables is concerned, no. But if you want to persist this design, it is possible with functions.

$str2="the new value is ".newValue();
function newValue()
{
  return 'something new';
}

echo $str2;
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.