seventheyejosh Posted July 8, 2009 Share Posted July 8, 2009 Hey guys, I was wondering if someone could tell me the proper way to use eval for this: $string='include "classes/$file";'; $string2='$name = new $name()'; I'm quite stumped and wondering if this is even possible :\ Thanks Quote Link to comment https://forums.phpfreaks.com/topic/165256-solved-eval-code/ Share on other sites More sharing options...
seventheyejosh Posted July 8, 2009 Author Share Posted July 8, 2009 bumpitty Quote Link to comment https://forums.phpfreaks.com/topic/165256-solved-eval-code/#findComment-871530 Share on other sites More sharing options...
seventheyejosh Posted July 9, 2009 Author Share Posted July 9, 2009 Final bump (cause its on page 3 atm ) before i resort to googling again. The first go round yielded very little Quote Link to comment https://forums.phpfreaks.com/topic/165256-solved-eval-code/#findComment-871940 Share on other sites More sharing options...
PFMaBiSmAd Posted July 9, 2009 Share Posted July 9, 2009 You might want to reread your question and ask yourself if it clearly indicates what you are asking. The quickest way to find out if some idea works in programming, is to try it. Quote Link to comment https://forums.phpfreaks.com/topic/165256-solved-eval-code/#findComment-871960 Share on other sites More sharing options...
seventheyejosh Posted July 9, 2009 Author Share Posted July 9, 2009 Ok I'll try again I've always stayed away from eval because it confuses me a little . I've read over php.net's examples many times now, but something still isn't clicking. I was just throwing out an example but i guess my question is, is it possible to run stored code from the data base? Like say I have this piece of code: $username=$_SESSION['user']; echo "Username is $username"; Now, is there a way for me to save that in a text field (or watever field is applicable) then query the DB, get that snippet, and run it as code? As far as I understood that is what eval is used for. I've tried and failed miserably to run it, moving around \ and " and $ to no avail in my evals . So my question is, if I had that string in the DB and I got it, like so: $res=mysql_query("SELECT * FROM `strings` WHERE `id`='1'"); $row=mysql_fetch_array($res); $string=$row['string']; String now has the aforementioned username/session code in it. So how can I tell php it is code and run it? Thanks to all, and sorry to ramble and confuse. Eval is one thing I've never been able to get! Quote Link to comment https://forums.phpfreaks.com/topic/165256-solved-eval-code/#findComment-871984 Share on other sites More sharing options...
thebadbad Posted July 9, 2009 Share Posted July 9, 2009 Um, eval($string); ? Example: <?php $code = '$test = "print this"; echo $test;'; eval($code); //print this ?> Quote Link to comment https://forums.phpfreaks.com/topic/165256-solved-eval-code/#findComment-872012 Share on other sites More sharing options...
seventheyejosh Posted July 9, 2009 Author Share Posted July 9, 2009 OH! So simple! I was trying to do like: $string='echo "blah";'; (from db) then like eval("\$string=\"$string\";"); echo $string; or something strange like that that wouldnt work :\ Thanks much Quote Link to comment https://forums.phpfreaks.com/topic/165256-solved-eval-code/#findComment-872018 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.