help with eval usage

help with eval usage

am 15.06.2007 21:19:50 von cronoklee

Can someone please give me a hand with this?

$arr=3Darray(2,4,5,6);

$test=3D'arr';

echo (eval("$".$test."[0]"));

I'm trying to get it to echo 2

Thanks a lot,
Ciar=E1n

Re: help with eval usage

am 15.06.2007 21:32:14 von Daniel Kempkens

Ciaran schrieb:
> Can someone please give me a hand with this?
>
> $arr=array(2,4,5,6);
>
> $test='arr';
>
> echo (eval("$".$test."[0]"));
>
> I'm trying to get it to echo 2
>
> Thanks a lot,
> Ciarán
>
Without trying it, this should work:
eval("echo $".$test."['0'];");

Re: help with eval usage

am 15.06.2007 21:39:19 von Moot

On Jun 15, 3:19 pm, Ciaran wrote:
> Can someone please give me a hand with this?
>
> $arr=3Darray(2,4,5,6);
>
> $test=3D'arr';
>
> echo (eval("$".$test."[0]"));
>
> I'm trying to get it to echo 2
>
> Thanks a lot,
> Ciar=E1n

Try this:
eval("echo $".$test."[0];");

Don't treat eval like a function that returns a string, make sure the
entire PHP statement is passed to it (including the ;).

An alternative, that looks like what you were trying to accomplish,
but without the eval would be:
echo ${$test}[0];

Re: help with eval usage

am 15.06.2007 21:49:54 von cronoklee

On Jun 15, 8:39 pm, Moot wrote:
> On Jun 15, 3:19 pm, Ciaran wrote:
>
> > Can someone please give me a hand with this?
>
> > $arr=3Darray(2,4,5,6);
>
> > $test=3D'arr';
>
> > echo (eval("$".$test."[0]"));
>
> > I'm trying to get it to echo 2
>
> > Thanks a lot,
> > Ciar=E1n
>
> Try this:
> eval("echo $".$test."[0];");
>
> Don't treat eval like a function that returns a string, make sure the
> entire PHP statement is passed to it (including the ;).
>
> An alternative, that looks like what you were trying to accomplish,
> but without the eval would be:
> echo ${$test}[0];


Oh great thanks , that works well. I'm used to the eval function in
flash actionscript and I just thought the php one would be the same.
What if I wanted to duplicate the original array?

$arr2=3D${$test};

??
Cheers

Re: help with eval usage

am 15.06.2007 21:53:47 von Daniel Kempkens

Ciaran schrieb:
> On Jun 15, 8:39 pm, Moot wrote:
>> On Jun 15, 3:19 pm, Ciaran wrote:
>>
>>> Can someone please give me a hand with this?
>>> $arr=array(2,4,5,6);
>>> $test='arr';
>>> echo (eval("$".$test."[0]"));
>>> I'm trying to get it to echo 2
>>> Thanks a lot,
>>> Ciarán
>> Try this:
>> eval("echo $".$test."[0];");
>>
>> Don't treat eval like a function that returns a string, make sure the
>> entire PHP statement is passed to it (including the ;).
>>
>> An alternative, that looks like what you were trying to accomplish,
>> but without the eval would be:
>> echo ${$test}[0];
>
>
> Oh great thanks , that works well. I'm used to the eval function in
> flash actionscript and I just thought the php one would be the same.
> What if I wanted to duplicate the original array?
>
> $arr2=${$test};
>
> ??
> Cheers
Works exactly the way you guessed ;)

Re: help with eval usage

am 15.06.2007 22:03:43 von luiheidsgoeroe

On Fri, 15 Jun 2007 21:19:50 +0200, Ciaran wrote=
:

> Can someone please give me a hand with this?
>
> $arr=3Darray(2,4,5,6);
>
> $test=3D'arr';
>
> echo (eval("$".$test."[0]"));
>
> I'm trying to get it to echo 2

Eval doesn't return anything if you don't tell it to. You could use a =

return though (the same as in function/methods/included files).

So, to echo it, you could use: echo eval('return $'.$test.'[0]');

The curly braces method as described by others is preferable though.
-- =

Rik Wasmus

Re: help with eval usage

am 15.06.2007 22:04:12 von cronoklee

On Jun 15, 8:53 pm, Daniel Kempkens wrote:
> Ciaran schrieb:
>
>
>
> > On Jun 15, 8:39 pm, Moot wrote:
> >> On Jun 15, 3:19 pm, Ciaran wrote:
>
> >>> Can someone please give me a hand with this?
> >>> $arr=3Darray(2,4,5,6);
> >>> $test=3D'arr';
> >>> echo (eval("$".$test."[0]"));
> >>> I'm trying to get it to echo 2
> >>> Thanks a lot,
> >>> Ciar=E1n
> >> Try this:
> >> eval("echo $".$test."[0];");
>
> >> Don't treat eval like a function that returns a string, make sure the
> >> entire PHP statement is passed to it (including the ;).
>
> >> An alternative, that looks like what you were trying to accomplish,
> >> but without the eval would be:
> >> echo ${$test}[0];
>
> > Oh great thanks , that works well. I'm used to the eval function in
> > flash actionscript and I just thought the php one would be the same.
> > What if I wanted to duplicate the original array?
>
> > $arr2=3D${$test};
>
> > ??
> > Cheers
>
> Works exactly the way you guessed ;)- Hide quoted text -
>
> - Show quoted text -

Great! That seems to be the functionality I was looking for with eval.
Thanks for the help guys!

Re: help with eval usage

am 15.06.2007 22:13:04 von gosha bine

Ciaran wrote:
> Can someone please give me a hand with this?
>
> $arr=array(2,4,5,6);
>
> $test='arr';
>
> echo (eval("$".$test."[0]"));
>
> I'm trying to get it to echo 2
>
> Thanks a lot,
> Ciarán
>

$arr=array(2,4,5,6);
$test='arr';
echo ${$test}[0];

no need of eval.


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok