Using eval() isn't working

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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Using eval() isn't working

Post by Chris Corbyn »

Hi,

Never used eval() before so maybe I'm doing something wrong here....

I'm trying to read the contents of a PHP script which by itself echo()'s content to the page. I need to somehow read the output from within another script and store it in a string. I don't want to have to use shell access or use local loopback with apache.

I figured that somehow eval() would be a tasty option here but it seems to reject anything I throw at it..

The code I want to eval() - in another file

Code: Select all

<?php
$foo = 'bar';
echo $foo;
?>
and...
The file I'm trying to do it from

Code: Select all

<?php
$output = eval(file_get_contents('path/to/the/file.php'));
$ret = 'Foo'.$output;
echo $ret;
?>
The error message wrote:Parse error: parse error, unexpected '<' in /home/d11wtq/public_html/shane2/sys/core/core.class.php(327) : eval()'d code on line 1
I've also tinkered around with output buffering with little luck. If anyone knows what I'm doing wrong or a way to do it better please say (I do need to get the output from the other file either way).

Cheers,

d11
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Try taking off the php tags in the file to be included. I think eval assume PHP mode.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

should be able to effectively use output buffering to capture the output from the when included ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Thanks nielsene that works but I have lots and lots of files that were previously called with the include() function.... now I want to capture the output in a string and if eval() won't allow <?php ?> tags it's probably the wrong route :?

feyd, do you mean use ob_ and simply stick include() in there? I hadn't thought of that... I'll try it.. ;)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

feyd, you're a genius!

If anyone's wondering...

Code: Select all

<?php
ob_start();
include('path/to/the/file.php');
$output = ob_get_contents();
ob_end_clean();
$ret = 'Foo'.$output;
echo $ret;
?>
Post Reply