alecks Posted April 22, 2007 Share Posted April 22, 2007 I want to do something like this" $name = "test"; if (function_exists (".$name."_function"){ $name."_function"(); } Basically, the prefix of the name of the function will be in a variable, and I want to use that to call a function. This may seem like a long way around to something, but I have reasons. Please don't give me a run around and tell me to do it another way, I'd just like to know if it can be done Thanks for any help, its very appreciated. Link to comment https://forums.phpfreaks.com/topic/48091-calling-functions-with-variables-in-the-name-can-it-be-done/ Share on other sites More sharing options...
Navarr Posted April 22, 2007 Share Posted April 22, 2007 $name = "test"; if (function_exists($name."_function") { eval($name."_function()"); } That should do what you want.. I think ._.;; Link to comment https://forums.phpfreaks.com/topic/48091-calling-functions-with-variables-in-the-name-can-it-be-done/#findComment-235020 Share on other sites More sharing options...
genericnumber1 Posted April 22, 2007 Share Posted April 22, 2007 There are a few syntax errors in your code, but I get the gist of what you're saying. this... <?php if (function_exists($name."_function")){ ?> ... works fine this... <?php $name."_function"(); ?> ... doesn't your alternative for the second one would be to set it to a variable and then call the function <?php $function = $name . '_function'; $function(); ?> ps. Avoid eval() like the plague! don't make me use overquoted cliche phrases to convince you... just avoid it Link to comment https://forums.phpfreaks.com/topic/48091-calling-functions-with-variables-in-the-name-can-it-be-done/#findComment-235021 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.