johnwry
asked on
execute functions using eval
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());
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());
or as eval:
$x = eval('return doit();');
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
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();
ob_start();
eval($row['code']);
$out1 = ob_get_contents();
ob_end_clean();
ASKER
the function gets outputted when i have this:
function doIt(){
$var ='test';
echo $var;
}
but I need this:
function doIt(){
$var ='test';
return $var;
}
function doIt(){
$var ='test';
echo $var;
}
but I need this:
function doIt(){
$var ='test';
return $var;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
The outputbuffer capatures all data written with echo and print. Then you can use the variable to process the result.
ASKER
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).
$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).
please post more information so we can make a complete solution for your need.
ASKER
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
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
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();
$query = 'include 'files.php'; doit();';
ob_start();
$contents .= eval("return $query");
$contents.= ob_get_contents();
ob_end_clean();
ASKER
it outputs it but because doit() is ending with echo it posts the contents above the rest of the page.
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
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;
ASKER
thanks for the suggestion. I'm too far into my this CMS to drop it now.
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
Open in new window