eval problem

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have to eval a string stored in a variable, but it's not working.

function ee($s)
    return $s;


echo $stri;

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

Re: eval problem

iulian.ilea wrote:
Quoted text here. Click to load it

Why use eval at all?

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


Re: eval problem

Janwillem Borleffs wrote:
Quoted text here. Click to load it

Thanks, but what I wrote above is just an example. I take the variable
that must be eval'd from an sql table.

Re: eval problem

iulian.ilea said the following on 24/09/2006 12:54:
Quoted text here. Click to load it

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  

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).


Re: eval problem

iulian.ilea wrote:

Quoted text here. Click to load it

Exactly the way its written in the manual (http://uk2.php.net/eval )

$v = 'e("name")';
$v="$stri = " . $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

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


Re: eval problem

Quoted text here. Click to load it

Well, for one thing, I would not try to call a function called "e" when I
had named the function "ee".
- Tim Roberts, timr@probo.com
  Providenza & Boekelheide, Inc.

Site Timeline