Is it possible to store eval() results in a variable?

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
liminalhigh
Forum Newbie
Posts: 3
Joined: Fri Aug 27, 2004 5:27 pm

Is it possible to store eval() results in a variable?

Post by liminalhigh »

I have a mix of HTML/PHP stored in mysql as a template. I run eval() to populate the variables from the template. I can get it to display/view properly, however I want to take the actual final, evaluated HTML and email it to myself.

It seems like all I would need to do is store the eval()'d code in a variable, and then include it in the basic mail() function.

Is this even possible? Is there some other way of doing this?

Thanks!
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

Something like:

Code: Select all

eval("\$variable = \$code;");
Where the php code and stuff is in the $code variable.
liminalhigh
Forum Newbie
Posts: 3
Joined: Fri Aug 27, 2004 5:27 pm

Post by liminalhigh »

Heyas,

I just tried this and for some reason it doesn't actually evaluate the code... just spits out the HTML/PHP from the db. :\
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

because you have it echoing out... [php_man]ob_start[/php_man]
liminalhigh
Forum Newbie
Posts: 3
Joined: Fri Aug 27, 2004 5:27 pm

Post by liminalhigh »

Sorry, but I don't get it. I'm not actually using echo anywhere... I am just trying to fit $variable into the mail() function:

for example:

Code: Select all

<?php
eval("\$strBody = \$template;");
mail ($strMailTo, $strSubject, $strBody, $strXHeaders);
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if you jump in and out of php inside this code, you have it echoing out. you need to capture it, via output buffering (ob_start)
ldomingues
Forum Commoner
Posts: 41
Joined: Fri Aug 06, 2004 1:15 pm
Location: Portugal

Post by ldomingues »

Can you post an exemple of what you have in $template ?
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Eval is a code smell. You rarely need it, and if you do you're probably doing something the wrong way.

I'd suggest storing templates as html files rather than in the db. That'll remove any need for eval as well as allowing you to open the html files in a wysiwyg editor or etc.
Post Reply