Hi all,

I'm developping a site that gets all it's html (includes php scripts) files from an sql database. I'm using the eval() function to good effect but have run into a problem, I can't include files as expected, and/or I can't use global variables as expected.

Example:

globals.php
$var=array('value1','value2'); // a neccessary global variable

index.php
include_once("globals.php");
somefunction($var);

$str="include_once("globals.php"); someotherfunction($var);";
eval($str); // $var is empty

$str="include("globals.php"); someotherfunction($var);";
eval($str); // error about multiple includes

$str="someotherfunction($var);";
eval($str); // $var is empty

So how do I get access to globals from withing eval or how do include files as expected (when $str is seperate file this all works properly)?

    Your second error (multiple definitions) suggests what the problem is: the contents of globals.php are already available inside the eval. Does it work if you leave the "evaled includes" out?

    And remember, if $var is supposed to be the argument to somefunction, and not its value, you'll need to escape its $. Without escaping it, the eval("somefunction($var);"); will evaluate
    somefunction(array('value1','value2')) - and there's no mention of $var in that.
    eval("somefunction(\$var);"); will evaluate somefunction($var);, if that's what you want.

      Not including the files doesn't help but it did tip me off to something.

      Functions in the included file work. ie: I can call them.

      When can you see other functions but not global variables? From within a function! So I added the line: global $var and lo and behold it worked!

      But this is not a viable solution as I would have to edit all existing pages and then add a 'global <all variables>' line. Then if I ever add/delete a variable I would have to re-edit all the pages again.

      Thanks for clue but do you happen to have any other ideas?

      Marqis

        After getting a decent nights sleep I have some progress to report. Since I'm calling eval() from inside another function MyEval() then it stands to reason that you don't have access to the global variables.

        Anyhow here's what I currently have that works, but is not at all eloquent.

        $var=array();  // global scope
        
        function MyEval()
        {
        global $var; // this makes it work but is ugly. What if my globals change?
        $str="somefunction($var)"; // $str is really a complete html page from a mysql database
        
        eval($str);
        }
        

        So anyhow, that works, only requires code changes in one spot if the globals change (as opposed to changing all the html pages) but still not nearly pretty enough.

        Is there a way to iterate over all user defined global variables and make them available? Why in the hell aren't global variables available in functions automatically like in every other language out there?!?

        So in pseudo-code:

        foreach($user_defined_variable as $uservar)
        eval("global $$uservar");
        

        Is this possible?

          Well, there is always the $GLOBALS[] array: global $var; is equivalent to $var=&$GLOBALS['var'];. There's nothing stopping you from using the array directly.

          As for your question:

          Why in the hell aren't global variables available in functions automatically like in every other language out there?!?

          Well, leaving aside the overgeneralisation (some languages don't even have variables) - one reason is because using global variables tends to reduce function portability.

            Write a Reply...