1

Imagine we have 2 files, one called 1.php with the following code:

<?php
    $hello = "Hello from 1";
?>

and 2.php with the following code:

<?php
    function LoadPage( $page )
    {
        $f = fopen( $page, 'r+' );  
        $content = fread( $f, filesize($page) );
        fclose( $f );
        return $content;
    }

    function GetEvalContent( $content )
    {
        $var = "";
        ob_start();
        eval( "?>" . $content . "<?" );
        $var = ob_get_contents();
        ob_end_clean();
        return $var;
    }

    $hello = "hello from 2";

    echo $hello . '<br/>';

    $content = LoadPage( '1.php' );
    GetEvalContent( $content );

    echo $hello;
?>

So what the 2.php does is load the content of 1.php and evaluate the php code inside it. Now what I want to do is during the evaluation of 1.php, variable $hello changes to "hello from 1". However if you execute 2.php you always get:

"hello from 2"
"hello from 2"

instead of getting

"hello from 2"
"hello from 1"

Has anyone encountered this problem before and if so, how would you fix it?

3
  • Just fyi eval( "?>" . $content . "<?" ); is not valid syntax. Commented Aug 3, 2011 at 21:45
  • It is a valid syntax. The whole point is to evaluate $content which has mixture of php code and non-php code. Please google it first! Commented Aug 3, 2011 at 22:10
  • Hmm, seems you're right. I didn't realize that, because I've been trained to avoid eval. Commented Aug 4, 2011 at 13:35

4 Answers 4

5

There is a much easier way to do this. Use PHP's include.

1.php

<?php
   $hello = "Hello from 1";
?>

2.php

<?php
   $hello = "hello from 2";
   echo $hello;
   include '1.php';
   echo $hello;
?>

UPDATE (not tested):

function includeFile($file){
  global $hello;  // Use the global variable $hello
                  // this will make the include sets $hello correctly
  ob_start();
  include $file;  // Remember any variables set here will be in this scope,
                  // not the global scope (unless you add them to the global line above)
  $var = ob_get_contents();  // This will contain anything echoed to the screen
                             // from the included file
  ob_end_clean();
  return $var;
}

$hello = "hello from 2";
echo $hello;
$file = '1.php';
$output = includeFile($file);
echo $hello;
echo $output;
Sign up to request clarification or add additional context in comments.

6 Comments

This wouldn't work if you want to dynamically change the name of the file. Imagine, based on user's action, you want to evaluate/load 4.php instead of 1.php.
@Ali: Yes it would. $file = '4.php'; include($file);
Well, I guess I didn't clear it enough. The whole point of that was to evaluate the file and store the result into a variable. if you use include or require, you will flush it to the browser.
@Ali: No, you did not explain that. In your example, you just showed setting a variable, not echoing anything to the screen. Try adding ob_start before the include then ob_get_contents and ob_end_clean after it.
Thanks, however, The problem is that I want to put all those into a single function which can be called in my code multiple times. Would it be possible to wrap all those into a function somehow?
|
1

You're doing your eval() within a function, so the $hello in the included file will be part of only the function's scope. It will not affect the $hello that's defined outside the function (which is global scope).

You'd need to put the global keyword into your included file, unless you want to write your own PHP parser to figure out what variables are being defined in the included file and auto-globalize them.

However, in the bigger picture... WHY? eval is a horribly evil ugly construct, and you're opening yourself up to a world of debugging pain, let alone the security issues.

6 Comments

Did not work! I have added global keyword before $hello in 1.php and it didn't work.
There's also the fact that eval( "?>" . $content . "<?" ); is not valid syntax.
if you have html and php code mixed, you have to use eval( "?>" . $content . "<?" );
@Ali: no. the eval'd text has to be valid PHP code, WITHOUT the ?>/<? delimiters. eval('Hello') would fail, but eval('$txt = 'Hello';) will work.
I disagree with you. The whole point of adding those delimiters is to evaluate an staring that has mixture of php code and non-php code.
|
1

Have you considered using require or include? PHP Manual

Example:

$hello = "Hello from 2";
echo $hello;
include("1.php");
echo $hello;

Comments

0

try to use $GLOBALS['hello'] instead of $hello

PS: Don't forget eval is evil ;)

3 Comments

Also, eval( "?>" . $content . "<?" ); is not valid syntax.
How about if I have mixture of HTML and PHP code? how would you run the code then?
@Ali: Turns out, that is valid. Weird.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.