Help - Search - Members - Calendar
Full Version: eval()
Invision Power Services > Community Forums > Community Web Design and Coding
Brendon Koz
I've seen plenty of coders use the eval() function, but I've never completely understood what it's actual use is for. Most code I've seen it used, it's somewhat redundant and I didn't see the need for it. Can anyone explain this to me?
Michael K.
It's used to simulate PHP code. For instance, when you want to use variables in places where you can't use variables. An example:

CODE
$type = 'private';
eval($type . ' $my_variable;');


It would declare a public variable. For a better explanation check the PHP manual on eval(). original.gif
Brendon Koz
I have the PHP Manual on my harddrive, both in separate HTML files for the webserver error messages, and in a CHM format for quick reference. I still don't really understand this function's usefulness.

The example given in the PHP Manual is as follows:
CODE
<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
?>


It's a horribly general description on the use of the function... I've tried looking at the online manual as people leave comments, but I still don't quite get it. Most things I've seen it used for can be much more easily done in other ways. However, I'm sure it has its definitive uses; I'd just like to understand where and why...

Thanks! original.gif
Rikki
If I remember correctly, Matt uses it in IPB since the safe-mode skins are stored as a class in the database. So when the class is pulled from the DB, it's run through eval() to execute it and 'make' the class real. So that's one use for it original.gif
Brendon Koz
Thanks Rikki - that's a use I can understand. The manual discussed that as a use, but I mis-understood it. I was thinking that the entire eval() statement was stored in the database. This helps me understand that use of it.

(Anyone know of any more?)
Michael_C
Rasmus Lerdorf(PHP creator): "If eval() is the answer, you're almost certainly asking the wrong question."
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.