Page 1 of 1
help using eval
Posted: Tue Oct 28, 2008 12:55 am
by s.dot
I've got the following array to test
testArray
Code: Select all
array(
'variable' => array(
'nested' => array(
'deep' => 'test array variable'
)
)
)
Which is stored in a class member $vars
Which means $this->vars['testArray']['variable']['nested']['deep'] = 'test array variable';
I have the following eval():
Code: Select all
$varString = "$this->vars['testArray']['variable']['nested']['deep']";
$varValue = eval("return \"$varString\";");
echo $varString . '<br />';
echo $varValue . '<br />';
Sadly it is returning this:
Code: Select all
Array[testArray][variable][nested][deep]
I want it to return
What am I doing wrong?
Re: help using eval
Posted: Tue Oct 28, 2008 1:31 am
by Christopher
scottayy wrote:What am I doing wrong?
I think you are misunderstanding eval(). The eval() funciton runs code, just as if it were parsed by include(). So you could do something like this:
Code: Select all
eval("$varValue = \"{$this->vars['testArray']['variable']['nested']['deep']}\";");
echo $varValue . '<br />';
Re: help using eval
Posted: Tue Oct 28, 2008 1:34 am
by requinix
Code: Select all
$varString = "$this->vars['testArray']['variable']['nested']['deep']";
It's a quoted string, so the $this->vars gets interpreted. The
['testArray']... bit would have worked, making $varString = "test array variable", except you're using the wrong syntax:
Code: Select all
"$this->vars[testArray][variable][nested][deep]" // no 's
"{$this->vars['testArray']['variable']['nested']['deep']}" // {..}s around the variable
Either of those are valid, but you used a hybrid of the two. So the parsing stops at the first [ and you're left with
Code: Select all
"Array['testArray']['variable']['nested']['deep']"
Then your eval'd statement simply returns that string (because the $ in $varString wasn't escaped so its value was used, not its name).
Assuming line 2 is correct (I see two ways of doing what you want) then your $varString needs to be one of the formats I mentioned.
Okay, so there are three ways: arborint posted the one I didn't consider.
Re: help using eval
Posted: Tue Oct 28, 2008 4:43 am
by Jenk
Code: Select all
$varValue = eval("return \"$varString\";");
should be:
Code: Select all
$varValue = eval("return $varString;");
Re: help using eval - SOLVED
Posted: Tue Oct 28, 2008 2:39 pm
by s.dot
arborint wrote:scottayy wrote:What am I doing wrong?
I think you are misunderstanding eval(). The eval() funciton runs code, just as if it were parsed by include(). So you could do something like this:
Code: Select all
eval("$varValue = \"{$this->vars['testArray']['variable']['nested']['deep']}\";");
echo $varValue . '<br />';
Yes, but it also returns a value if 'return' is used, so therefore I was able to assign it to a variable.
tasairis wrote:It's a quoted string, so the $this->vars gets interpreted. The ['testArray']... bit would have worked, making $varString = "test array variable", except you're using the wrong syntax:
Yes, you're right. Unfortunately I just posted a quick sample of what I was doing and didn't think about that. lol. I'm using the correct quotes in my app. Sorry about that!
Jenk's solution worked! Thanks a lot to all of you guys
