help using eval

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

help using eval

Post 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

Code: Select all

test array variable
What am I doing wrong?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: help using eval

Post 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 />';
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: help using eval

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: help using eval

Post by Jenk »

Code: Select all

$varValue = eval("return \"$varString\";");
should be:

Code: Select all

$varValue = eval("return $varString;");
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: help using eval - SOLVED

Post 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 :)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply