Zimi Posted August 9, 2008 Share Posted August 9, 2008 A friend of mine has a problem, he wants to be able to store function names inside variable and then be able to use the functions that are stored in the variables. How can he do that? Quote Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/ Share on other sites More sharing options...
wildteen88 Posted August 9, 2008 Share Posted August 9, 2008 Use $mvar(); However why would want to store function names in a variable? Make sure these variables are not set by the user otherwise you could cause security exploits in your code. Quote Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/#findComment-612456 Share on other sites More sharing options...
Zimi Posted August 9, 2008 Author Share Posted August 9, 2008 Use $mvar(); However why would want to store function names in a variable? Make sure these variables are not set by the user otherwise you could cause security exploits in your code. Don't know why he need it Oo Can you please explain me about $mvar()? google and php.net gives me nothing =p Quote Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/#findComment-612461 Share on other sites More sharing options...
wildteen88 Posted August 9, 2008 Share Posted August 9, 2008 Umm, $myvar is your actual variable, the () is so PHP calls the function eg $var = 'hello'; function hello() { return 'hello world'; }; echo $var(); Quote Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/#findComment-612463 Share on other sites More sharing options...
.josh Posted August 9, 2008 Share Posted August 9, 2008 Umm, $myvar is your actual variable, the () is so PHP calls the function eg $var = 'hello'; function hello() { return 'hello world'; }; echo $var(); Hey wow I didn't know you could do that; learn somethin' new every day I was just gonna suggest eval() Quote Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/#findComment-612465 Share on other sites More sharing options...
cooldude832 Posted August 9, 2008 Share Posted August 9, 2008 In a primitive example it doesn't show much use. the only realistic example is in a need to have a function be executed based off a user's input but even then a switch can be the same or better assuming a reasonable number of choices. Why use eval?? Quote Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/#findComment-612466 Share on other sites More sharing options...
Zimi Posted August 9, 2008 Author Share Posted August 9, 2008 Umm, $myvar is your actual variable, the () is so PHP calls the function eg $var = 'hello'; function hello() { return 'hello world'; }; echo $var(); Oh, I got it, how stupid am I XD Thanks ^^ Quote Link to comment https://forums.phpfreaks.com/topic/118944-solved-function-name-inside-a-variable/#findComment-612494 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.