1

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?

4
  • Isn't a string reassembling easier? sprintf() Commented Jul 5, 2013 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. Commented Jul 5, 2013 at 2:20
  • I have added a use case. Commented Jul 5, 2013 at 3:04
  • OK I can just do a string replace inside the loop str_replace("$key",$key,$myString), etc. for each value Commented Jul 5, 2013 at 3:20

2 Answers 2

2

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.

1
  • This does what I needed, but I appreciate that eval is not the safest method to use. Commented Jul 5, 2013 at 2:33
2

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.