stylusrose Posted May 16, 2008 Share Posted May 16, 2008 is there anyway to execute a string as code in php? other than saving $string to an outside file and then including that file and running it. just like: <?php $string = "echo( "potatoes" );" run $string; ?> Link to comment https://forums.phpfreaks.com/topic/105927-solved-string-as-code/ Share on other sites More sharing options...
conker87 Posted May 16, 2008 Share Posted May 16, 2008 $string = "potatoes"; echo $string; Like that? Link to comment https://forums.phpfreaks.com/topic/105927-solved-string-as-code/#findComment-542828 Share on other sites More sharing options...
947740 Posted May 16, 2008 Share Posted May 16, 2008 Based on what you are trying to do, I would use a function. <?php writeString($string) { echo $string; } // $string is what you want to echo ?> However, that would be kind of pointless when you can just echo it. Link to comment https://forums.phpfreaks.com/topic/105927-solved-string-as-code/#findComment-542830 Share on other sites More sharing options...
RichardRotterdam Posted May 16, 2008 Share Posted May 16, 2008 i think you where looking for the eval() function just be carefull on what you use it on it could be a security breach of you use it for a website Link to comment https://forums.phpfreaks.com/topic/105927-solved-string-as-code/#findComment-542834 Share on other sites More sharing options...
kenrbnsn Posted May 16, 2008 Share Posted May 16, 2008 I believe what the OP is looking for is the eval() function. Use it with care. Ken (beaten to it) Link to comment https://forums.phpfreaks.com/topic/105927-solved-string-as-code/#findComment-542835 Share on other sites More sharing options...
stylusrose Posted May 16, 2008 Author Share Posted May 16, 2008 Aha, there we are. Thanks a bunch! Link to comment https://forums.phpfreaks.com/topic/105927-solved-string-as-code/#findComment-542839 Share on other sites More sharing options...
947740 Posted May 16, 2008 Share Posted May 16, 2008 I am curious...what exactly does the eval() function do? Link to comment https://forums.phpfreaks.com/topic/105927-solved-string-as-code/#findComment-542846 Share on other sites More sharing options...
jonsjava Posted May 16, 2008 Share Posted May 16, 2008 from php.net: Evaluates the string given in code_str as PHP code. Among other things, this can be useful for storing code in a database text field for later execution. There are some factors to keep in mind when using eval(). Remember that the string passed must be valid PHP code, including things like terminating statements with a semicolon so the parser doesn't die on the line after the eval(), and properly escaping things in code_str . To mix HTML output and PHP code you can use a closing PHP tag to leave PHP mode. Also remember that variables given values under eval() will retain these values in the main script afterwards. Link to comment https://forums.phpfreaks.com/topic/105927-solved-string-as-code/#findComment-542851 Share on other sites More sharing options...
947740 Posted May 16, 2008 Share Posted May 16, 2008 I was hoping for a non technical explanation (it was not too bad), but it did help explain it a little bit. Link to comment https://forums.phpfreaks.com/topic/105927-solved-string-as-code/#findComment-542858 Share on other sites More sharing options...
conker87 Posted May 16, 2008 Share Posted May 16, 2008 Evaluates the given string as php code. Link to comment https://forums.phpfreaks.com/topic/105927-solved-string-as-code/#findComment-542860 Share on other sites More sharing options...
947740 Posted May 16, 2008 Share Posted May 16, 2008 That is pretty much exactly the same as the first sentence in the quote from the website. Link to comment https://forums.phpfreaks.com/topic/105927-solved-string-as-code/#findComment-542863 Share on other sites More sharing options...
stylusrose Posted May 16, 2008 Author Share Posted May 16, 2008 basically you can put php functions into the string, and then using the string like you would the "include" statement Link to comment https://forums.phpfreaks.com/topic/105927-solved-string-as-code/#findComment-542875 Share on other sites More sharing options...
947740 Posted May 16, 2008 Share Posted May 16, 2008 I understand what it means now, thanks. Link to comment https://forums.phpfreaks.com/topic/105927-solved-string-as-code/#findComment-542944 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.