Jump to content

Calling functions with variables in the name. Can it be done?


alecks

Recommended Posts

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 :D

 

Thanks for any help, its very appreciated.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.