Solved

execute functions using eval

Posted on 2008-06-19
19
192 Views
Last Modified: 2013-12-12
Hello,
I'm trying to get eval to execute functions but it's not working....that is I need it to be returned and not echoed.

I need this:
function doIt(){
$var = 'this is what i would like to output';
return $var;
}
eval(doit());
0
Comment
Question by:johnwry
  • 6
  • 6
  • 4
  • +1
19 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 21820995
You don't need eval() to get the return value back.  This should be enough...
function doIt(){
    $var = 'this is what i would like to output';
    return $var;
}
$my_value = doit();

Open in new window

0
 
LVL 48

Expert Comment

by:hernst42
ID: 21821005
or as eval:
$x = eval('return doit();');

Open in new window

0
 
LVL 48

Accepted Solution

by:
hernst42 earned 168 total points
ID: 21821016
or if output is one via eval use:

ob_start();
eval('doit();');
$out1 = ob_get_contents();
ob_end_clean();

http://www.php.net/manual/en/ref.outcontrol.php
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 27

Assisted Solution

by:yodercm
yodercm earned 166 total points
ID: 21821023
function doIt(){
$var = 'this is what i would like to output';
return $var;
}
eval(doit());

NOTE that you have spelled the function doIt (with a capital I) and then invoked the function doit().

As the others said, don't use eval(), it's for something else entirely.

function doIt(){
$var = 'this is what i would like to output';
return $var;
}

$varvalue = doIt();
0
 

Author Comment

by:johnwry
ID: 21821059
I understand that eval() is extremely dangerous and slow so should not be dealt with lightly.
My problem is that I am (force to) storing php functions in mysql and would like to execute those functions.  At the moment I am able to execute normal code and this works fine but I would really like to execute a function instead of storing huge amounts into mysql.
0
 
LVL 48

Expert Comment

by:hernst42
ID: 21821075
So if the code comes from a database you can use something like this:

ob_start();
eval($row['code']);
$out1 = ob_get_contents();
ob_end_clean();
0
 

Author Comment

by:johnwry
ID: 21821120
the function gets outputted when i have this:

function doIt(){
$var ='test';
echo $var;
}
but I need this:
function doIt(){
$var ='test';

return $var;
}

0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 166 total points
ID: 21821145
That echo() function writes to the browser output stream, unless you use output buffering.

try something like this:
ob_start();
doIt();
$var = ob_get_contents();
ob_end_clean();

Open in new window

0
 
LVL 48

Expert Comment

by:hernst42
ID: 21821160
Then use the output buffer as I already posted in http:#21821075

The outputbuffer capatures all data written with echo and print. Then you can use the variable to process the result.
0
 

Author Comment

by:johnwry
ID: 21821307
This almost works:
$query  = 'include 'files.php'; doit();';
$x = eval('return $query');

Problem is I have other code to run not just doit();

This does work:
$query  = 'include 'files.php'; doit();';
ob_start();
eval($query);
$contents.= ob_get_contents();
ob_end_clean();

but it doesn't allow for any other code to be outputted and $contents.= needs to be all my page (it's running through an engine).
0
 
LVL 48

Expert Comment

by:hernst42
ID: 21821328
please post more information so we can make a complete solution for your need.
0
 

Author Comment

by:johnwry
ID: 21821425
ok. sorry. Didn't think it would get this complicated.

What I have is a template engine and $contents is  the main content of the page.
It works fine except for now I want to develop a blog where each user can have their own url: mysite.com/user.html .
So, i have processed everything to where IF the page is user.html THEN we get the php code from mysql and run it:
$php = "includes 'file.php'; doit();"

$contents.= eval($php);

That WOULD have worked if it wasn't that I need to have the doit() function in there. If it was just php code it runs fine. At the moment if the function doit() ends with echo then the $contents output is placed outside the rest of the page. IF the function doit() ends with return then nothing is outputted.
hope this helps to clarify
0
 
LVL 48

Expert Comment

by:hernst42
ID: 21821458
ok so you want to capute both parts, so try

$query  = 'include 'files.php'; doit();';
ob_start();
$contents .= eval("return $query");
$contents.= ob_get_contents();
ob_end_clean();
0
 

Author Comment

by:johnwry
ID: 21821827
it outputs it but because doit() is ending with echo it posts the contents above the rest of the page.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 21849037
John: Couple of things...

First you might try the code snippet.  

Second you might consider installing multiple instances of WordPress in different directories for each user who wants a blog.  It's easy enough to do and you get the heavy lifting already done for you by the wordpress community.

Just a thought, ~Ray
ob_start();
$query  = "include ('files.php'); doit();";
$contents .= eval("return $query");
$contents.= ob_get_contents();
ob_end_clean();
echo $contents;

Open in new window

0
 

Author Comment

by:johnwry
ID: 22510925
thanks for the suggestion. I'm too far into my this CMS to drop it now.


0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 22511045
Well, good luck with the project.  In any case, I think we have answered your questions about eval() and hopefully shown you many ways to achieve the objectives.  ~Ray
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question