473,796 Members | 2,703 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 1782
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...@hotm ail.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...@mootsof t.comwrote:
On Jun 15, 3:19 pm, Ciaran <cronok...@hotm ail.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...@mootsof t.comwrote:
>On Jun 15, 3:19 pm, Ciaran <cronok...@hotm ail.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*******@hotm ail.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.comwrot e:
Ciaran schrieb:
On Jun 15, 8:39 pm, Moot <use...@mootsof t.comwrote:
On Jun 15, 3:19 pm, Ciaran <cronok...@hotm ail.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
2086
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 existing method within the class, and $errDisplayArray will be an array element containing text that goes appropriately into the class method whose name is in $val.
12
8209
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 this: $valid = checkPHP($string); if ($valid) { eval($string); } else { $resultsObject->addToErrorResults("We wanted to send our template to eval(), but the PHP it contained was invalid.");
6
2511
by: amohajer | last post by:
what does eval function do in javascript language and what are its usages?
3
2571
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 use the calculator. "document.Calculate.Input is null or not an object" Can someone please tell me why this is?
24
3440
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 makes Ajax request to ASP.Net web service. Web service does some data lookup and builds a string representation of a Javascript array which is then returned to the client. In the ajax callback, call to eval on the returned string and voila, ...
3
375
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - When should I use eval? ----------------------------------------------------------------------- The ` eval() ` function should _only_ be used when it is necessary to evaluate a string supplied or composed at run-time; the string can be anything from a simple (but unpredictable) expression such as 12*2.54 to a substantial piece of javascript code.
4
1988
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
3933
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. My problem is that when I pull the data (currently stored as a dictionary on the site) off the site, it is a string, I can use eval() to make that string into a dictionary, and everything is great. However, this means that I am using eval() on...
4
2127
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 make code run slowly. The following works fine on my PC, however I do not know what will happen on other platforms, so is there an alternative to "Eval" in this instance. for(var i=1;i<505;i++){
0
9531
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10187
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9055
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7553
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6795
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3735
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.