473,511 Members | 14,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

help with eval usage

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

Jun 15 '07 #1
7 1765
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'];");
Jun 15 '07 #2
On Jun 15, 3:19 pm, Ciaran <cronok...@hotmail.comwrote:
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];

Jun 15 '07 #3
On Jun 15, 8:39 pm, Moot <use...@mootsoft.comwrote:
On Jun 15, 3:19 pm, Ciaran <cronok...@hotmail.comwrote:
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

Jun 15 '07 #4
Ciaran schrieb:
On Jun 15, 8:39 pm, Moot <use...@mootsoft.comwrote:
>On Jun 15, 3:19 pm, Ciaran <cronok...@hotmail.comwrote:
>>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 ;)
Jun 15 '07 #5
Rik
On Fri, 15 Jun 2007 21:19:50 +0200, Ciaran <cr*******@hotmail.comwrote:
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
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
Jun 15 '07 #6
On Jun 15, 8:53 pm, Daniel Kempkens <Daniel.Kempk...@gmail.comwrote:
Ciaran schrieb:
On Jun 15, 8:39 pm, Moot <use...@mootsoft.comwrote:
On Jun 15, 3:19 pm, Ciaran <cronok...@hotmail.comwrote:
>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 ;)- Hide quoted text -

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

Jun 15 '07 #7
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
Jun 15 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2071
by: Phil Powell | last post by:
I have this line in my code: eval('$field->' . $val . '(' . $max . ',' . $errDisplayArray . ');'); where $field is a predefined class instance, $val is a string containing the name of an...
12
8144
by: lawrence | last post by:
I have a string which I want to send to eval(). How can I test it ahead of time to make sure it is valid code? I don't want to send it to eval and get parse errors. I want to do something like...
6
2493
by: amohajer | last post by:
what does eval function do in javascript language and what are its usages?
3
2545
by: Sean McCourt | last post by:
Hi I am doing a JavaScript course and learning from the recommed book (JavaScript 3rd Edition by Don Gosslin) Below is one of the exercises from the book. I get this error message when I try to...
24
3392
by: Larry | last post by:
Hi there: I have seen numerous postings about eval() and its evils on this forum. However, one of our developers is using it in the following way, which seems like a great use of it. Page...
3
375
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - When should I use eval? ----------------------------------------------------------------------- The ` eval() `...
4
1968
by: Adam C. | last post by:
Mozilla.org suggests using the with statement to control bindings: var f = 2; with({f: 3}){ eval("f"); // evaluates to 3 } But that doesn't work for binding "this".
16
3907
by: Fett | last post by:
I am creating a program that requires some data that must be kept up to date. What I plan is to put this data up on a web-site then have the program periodically pull the data off the web-site. ...
4
2107
by: spamme | last post by:
Hi, As you can see from the following code, I am trying to manipulate a large number of layers. I have read many times in the past how the "Eval" function should be used very sparingly as it can...
0
7252
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7371
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7517
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4743
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1583
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.