tmh766 Posted April 6, 2009 Share Posted April 6, 2009 Is this possible? For example $variable="functionOne('param1','param2')"; Another example $varaible2="functionOne(functionTwo(1))"; Is it possible to execute $variable which would then call functionOne and pass it its parameters? And is it possible to execute $variable2 which would call functionOne which would call functionTwo? Link to comment https://forums.phpfreaks.com/topic/152881-call-a-function-with-parameters-stored-within-a-variable-as-a-string/ Share on other sites More sharing options...
Maq Posted April 6, 2009 Share Posted April 6, 2009 What gives you doubt? Have you tried it? Link to comment https://forums.phpfreaks.com/topic/152881-call-a-function-with-parameters-stored-within-a-variable-as-a-string/#findComment-802855 Share on other sites More sharing options...
tmh766 Posted April 6, 2009 Author Share Posted April 6, 2009 I don't understand how to execute $variable, $variable() would not work as its not just a function name, but one with parameters. Link to comment https://forums.phpfreaks.com/topic/152881-call-a-function-with-parameters-stored-within-a-variable-as-a-string/#findComment-802867 Share on other sites More sharing options...
kaf723 Posted April 6, 2009 Share Posted April 6, 2009 Dude, it's so easy, it's like one line of code. It works bro! Link to comment https://forums.phpfreaks.com/topic/152881-call-a-function-with-parameters-stored-within-a-variable-as-a-string/#findComment-802879 Share on other sites More sharing options...
kaf723 Posted April 6, 2009 Share Posted April 6, 2009 Okay dude, like , for example, an MD5 Hash generatePassword(){ your code; } $variable1=md5(generatePassword()); Link to comment https://forums.phpfreaks.com/topic/152881-call-a-function-with-parameters-stored-within-a-variable-as-a-string/#findComment-802884 Share on other sites More sharing options...
Maq Posted April 6, 2009 Share Posted April 6, 2009 Here's another basic example: function f1($var1){ $what = $var1; return $what; } function f2($var2){ $what2 = $var2 + 2; return $what2; } $variable = f1(f2(1)); echo $variable; ?> As long as f2 returns something it will pass it to f1 which returns $what and variable is assigned to it. Link to comment https://forums.phpfreaks.com/topic/152881-call-a-function-with-parameters-stored-within-a-variable-as-a-string/#findComment-802890 Share on other sites More sharing options...
tmh766 Posted April 6, 2009 Author Share Posted April 6, 2009 Hey I can't do it like that though because the function is a String. What I was looking for was eval(), I can call eval($variable) so as long as $variable had return ahead of the function. Link to comment https://forums.phpfreaks.com/topic/152881-call-a-function-with-parameters-stored-within-a-variable-as-a-string/#findComment-802899 Share on other sites More sharing options...
premiso Posted April 6, 2009 Share Posted April 6, 2009 I am having a hard time understanding what the user is getting at. But why not use the call_user_func method? That is what it was intended for, from what I gathered on the OP. Link to comment https://forums.phpfreaks.com/topic/152881-call-a-function-with-parameters-stored-within-a-variable-as-a-string/#findComment-802908 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.