eval limitation or scope issue?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Rooster242
Forum Newbie
Posts: 18
Joined: Thu Aug 31, 2006 6:23 pm

eval limitation or scope issue?

Post by Rooster242 »

first_file.php:

Code: Select all

echo( "This is var: ".$var );
second_file.php:

Code: Select all

$var = "blah";
$text = file_get_contents( "first_file.php" );
eval( $text );
ouput:

Code: Select all

This is var:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

I cannot reproduce this behaviour.
PHP 5.1.5 (cli) (built: Aug 15 2006 23:54:56) wrote:This is var: blah
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

what version of php are you running?
Rooster242
Forum Newbie
Posts: 18
Joined: Thu Aug 31, 2006 6:23 pm

Post by Rooster242 »

The Ninja Space Goat wrote:what version of php are you running?
5.1.4
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Is there more code around the snippet you've posted?
Rooster242
Forum Newbie
Posts: 18
Joined: Thu Aug 31, 2006 6:23 pm

Post by Rooster242 »

i found the problem. i was actually using

Code: Select all

$text = file_get_contents( "http://localhost/firstfile.php" );
so my web server is evaluating the php in firstfile.php before the eval() call and before $var is defined.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you don't know exactly what you're doing, steer clear of using eval(). In many hands, both inexperienced and experienced, it is a dangerous function that can get you into a lot of problems.
Rooster242
Forum Newbie
Posts: 18
Joined: Thu Aug 31, 2006 6:23 pm

Post by Rooster242 »

yeah, i'm starting to realize that. but it seems to be the only solution for what i'm trying to do unless i want to do some wierd token replacement and pass a uri to file_get_contents() instead of a local path.

see viewtopic.php?t=54763
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Why can't you use require_once?
Rooster242
Forum Newbie
Posts: 18
Joined: Thu Aug 31, 2006 6:23 pm

Post by Rooster242 »

ole wrote:Why can't you use require_once?
because i'm doing other things with the file before i let the web server have it, like replacing strings for different languages etc...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Sounds like a redesign is in order.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Try using a proper file path rather than an http wrapper.
Post Reply