473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

eval problem

Hi,
I have to eval a string stored in a variable, but it's not working.
code:
___

function ee($s)
{
return $s;
}

$v="e(\"name\")";

eval("$stri=$v;");
echo $stri;
___

By using eval I want to eval $v and get the result returned by ee
stored in $stri. How woul you do?

Sep 23 '06 #1
5 1487
iulian.ilea wrote:
By using eval I want to eval $v and get the result returned by ee
stored in $stri. How woul you do?
Why use eval at all?

function ee($s) { return $s; }
$str = ee("Bob");
print $str;
JW
Sep 24 '06 #2

Janwillem Borleffs wrote:
iulian.ilea wrote:
By using eval I want to eval $v and get the result returned by ee
stored in $stri. How woul you do?

Why use eval at all?

function ee($s) { return $s; }
$str = ee("Bob");
print $str;
JW
Thanks, but what I wrote above is just an example. I take the variable
that must be eval'd from an sql table.

Sep 24 '06 #3
iulian.ilea said the following on 24/09/2006 12:54:
Janwillem Borleffs wrote:
>iulian.ilea wrote:
>>function ee($s)
{
return $s;
}

$v="e(\"name\")";

eval("$stri=$v;");
echo $stri;
>>By using eval I want to eval $v and get the result returned by ee
stored in $stri. How woul you do?
Why use eval at all?

function ee($s) { return $s; }
$str = ee("Bob");
print $str;

Thanks, but what I wrote above is just an example. I take the variable
that must be eval'd from an sql table.
This leads to a further question: Why does your database contain PHP
code? This goes against normal practices of both database and PHP
design. You may be aware of the popular quotation:

"If eval() is the answer, you're almost certainly asking the wrong
question."

i.e. there's probably a better, safer, more programmatic way of doing
what you're doing.
However, the answer to your original problem is that if you want the
string literal '$stri', you must escape the $ character, i.e.:

eval("\$stri = $v;");

Otherwise the value of $stri is substituted into the expression to be
evaluated (and presumably at this point $stri doesn't exist).
--
Oli
Sep 24 '06 #4
iulian.ilea wrote:
>
By using eval I want to eval $v and get the result returned by ee
stored in $stri. How woul you do?
Exactly the way its written in the manual (http://uk2.php.net/eval)

$v = 'e("name")';
$v="\$stri = " . $v . ";";
eval($v);
print $stri;

Eval (and storing code in a database) is very dangerous unless you know what
you're doing - and anyone who know what they were doing would RTFM first.
You have not done that therefore we must assume you don't know what you are
doing.

And if you did know what you were doing you would be trying very hard to
avoid the scenario where this is the solution.

C.
Sep 24 '06 #5
"iulian.ilea" <iu*********@gmail.comwrote:
>
I have to eval a string stored in a variable, but it's not working.
code:
___

function ee($s)
{
return $s;
}

$v="e(\"name\")";

eval("$stri=$v;");
echo $stri;
___

By using eval I want to eval $v and get the result returned by ee
stored in $stri. How woul you do?
Well, for one thing, I would not try to call a function called "e" when I
had named the function "ee".
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Sep 25 '06 #6

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

Similar topics

33
by: Stuart | last post by:
why won't the following work for(var i=0;i<pics;i++){ eval('img'+i) = new Image(wth,hgt) eval('img'+i+'.src') = 'http://www.mypics/'+i+'1.gif' } basically I am trying to create a numer of...
7
by: Reply Via Newsgroup | last post by:
This might sound sad... someone requesting a disertation on the 'eval' statement... but... I've been reading someone else's post - they had a huge calander like script and a handful of folk cursed...
6
by: Roebie | last post by:
Hi everyone, I'm having some weird problem with evaluating the continue statement. Within a for loop I'm trying to evaluate a string (generated somewhere earlier) which basically has the continue...
18
by: Joe Fallon | last post by:
I have some complex logic which is fairly simply to build up into a string. I needed a way to Eval this string and return a Boolean result. This code works fine to achieve that goal. My...
8
by: santel_helvis | last post by:
Hi, Here is my code. function CallDisplay() { nodeobj=new mynode(); x="displayId("+nodeobj+")"; eval(x);
3
by: Pauljh | last post by:
Hi All, I'm running some javascript over a server side generated web page and have multiple generated empty select statements, that I want to populate when the page is loaded. As HTML doesn't do...
6
by: RandomElle | last post by:
Hi there I'm hoping someone can help me out with the use of the Eval function. I am using Access2003 under WinXP Pro. I can successfully use the Eval function and get it to call any function with...
5
by: wendallsan | last post by:
Hi all, I'm running into a situation where it seems that JS stops executing as soon as I call an eval in my script. I have an Ajax.Request call to a PHP page that builds a JS object and returns...
12
by: Bill Mill | last post by:
Hello all, I want to have a user able to eval code in a text box. However, if he accidentally types "while(1) { i=0; }" and hits "run", I also want him to be able to hit a stop button such that...
16
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. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...

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.