Jump to content

[SOLVED] Can I use eval() to interpret code lines from a file?


stuntpeople

Recommended Posts

I have a file called 'news.php' from which I want to pull lines of text, inside which are some variables that are pre-assigned. As an example below, $movie_project_01 = "Titanic" and $current_location = "San Francisco"

 

news.php

10/29/07: I've just finished $movie_project_01 and it's been great.<BR><BR>
10/24/07: I've changed our location to $current_location.<BR><BR>
etc
etc
etc
etc

 

 

In my 'index.php' page, I have the following function that pulls $length lines from a file $filename and echoes each one

 

index.php

function getitems($filename, $length) {
    if (is_file($filename)) {
        $lines = file($filename);
            foreach ($lines as $line_num => $line) {
                if ($length != 0) {
                    if ($line_num >= $length) { break;
                }
            }
            echo $line;
        }
    }
}

 

The problem is when I have variables or any other php logic in my 'news.php' file, I can't seem to echo it properly. So if I call getitems('news.php', 2) I want to echo:

 

10/29/07: I've just finished Titanic and it's been great.<BR><BR>
10/24/07: I've changed our location to San Francisco.<BR><BR>

 

Instead it's doing this:

 

10/29/07: I've just finished $movie_project_01 and it's been great.<BR><BR>
10/24/07: I've changed our location to $current_location.<BR><BR>

 

Does anyone know how to do this properly? I can't seem to use eval() on it, but perhaps I'm not using it properly.

Link to comment
Share on other sites

You'd have to use eval in this situation. Working code:

<?php

// your vars here
// myVars format:
//    $myVars['variable_name_here'] = 'variable value';
$myVars['movie_project_01'] = 'movie01';
$myVars['current_location'] = '(Unknown Location)';

function getitems($filename, $length)
{
    global $myVars; // grab your variables.

    if (is_file($filename))
    {
        $lines = file($filename);

        foreach ($lines as $line_num => $line)
        {
            if ($length != 0)
            {
                if ($line_num >= $length)
                {
                    break;
                }

                extract($myVars); // get variables out of the array.

                eval("echo \"$line\";");
            }
        }
    }
}

You're best of defining all the variables you'll use in news.php within an array, I called this myVars. That way all you have to do is set $myVars as global rather than all your variables.

Link to comment
Share on other sites

Okay I'll be more specific then.

 

In the 'index.php' page, before the function 'getitems()' is a line,

 

include 'vars.php';

 

In 'vars.php':.

 

vars.php

$movie_project_01 = 'Titanic';
$current_location = 'San Francisco';

 

Shouldn't these variables be included already? Here's the error I receive when using eval();

 

c:\program files\easyphp1-8\www\index.php(12) : eval()'d code on line 1

Link to comment
Share on other sites

Okay I'll be more specific then.

 

In the 'index.php' page, before the function 'getitems()' is a line,

 

include 'vars.php';

 

In 'vars.php':.

 

vars.php

$movie_project_01 = 'Titanic';
$current_location = 'San Francisco';

 

Shouldn't these variables be included already? Here's the error I receive when using eval();

 

c:\program files\easyphp1-8\www\index.php(12) : eval()'d code on line 1

That is fine however in your function (getitems) you'll need to define all those variables in vars.php as global. You cannot use variables within a function that is defined outside of it, unless you define it as global (within the function) or your pass that variable as an argument. Functions have there own variable scope. You'll want to read up on variable scrope in order to understand how variables work with functions.

 

Incorrect:

$myvar = 'hello world';

function myfunc() {
    echo $myvar;
}

myfunc() // produces undefined variable 'myvar' notice

 

Correct

$myvar = 'hello world';

function myfunc() {
    global $myvar; // tell the function myvar is a global variable.

    echo $myvar;
}

myfunc() // produces hello world

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.