如何从带有参数的变量中存储的字符串调用PHP函数
我发现有人问我。但我需要用参数调用函数名。 我需要能够调用函数,但函数名存储在变量中,这可能吗?e、 g:如何从带有参数的变量中存储的字符串调用PHP函数,php,Php,我发现有人问我。但我需要用参数调用函数名。 我需要能够调用函数,但函数名存储在变量中,这可能吗?e、 g: function foo ($argument) { //code here } function bar ($argument) { //code here } $functionName = "foo"; $functionName($argument);//Call here foo function with argument // i need to call the
function foo ($argument)
{
//code here
}
function bar ($argument)
{
//code here
}
$functionName = "foo";
$functionName($argument);//Call here foo function with argument
// i need to call the function based on what is $functionName
任何帮助都将不胜感激。哇,一个拥有4枚金牌的用户不会提出这样的问题。你的代码已经运行了
<?php
function foo ($argument)
{
echo $argument;
}
function bar ($argument)
{
//code here
}
$functionName = "foo";
$argument="Joke";
$functionName($argument); // works already, might as well have tried :)
?>
哇,一个拿着4块金牌的用户不会问这样的问题。你的代码已经运行了
<?php
function foo ($argument)
{
echo $argument;
}
function bar ($argument)
{
//code here
}
$functionName = "foo";
$argument="Joke";
$functionName($argument); // works already, might as well have tried :)
?>
如果要使用参数动态调用函数,可以尝试如下操作:
function foo ($argument)
{
//code here
}
call_user_func('foo', "argument"); // php library funtion
希望它对您有所帮助。如果您想使用参数动态调用函数,那么您可以这样尝试:
function foo ($argument)
{
//code here
}
call_user_func('foo', "argument"); // php library funtion
希望对您有所帮助。您可以使用php函数
如果您在一个类中,您可以使用:
您可以使用php函数
如果您在一个类中,您可以使用:
我需要调用像$this->function\u name
这样的函数,我该怎么做呢?这也可以,只需稍加修改<代码>$this->$functionName()代码>对不起!问一个愚蠢的问题,但这对我的项目来说是紧急情况。不管怎样,它工作得很好。谢谢这个问题本身并不愚蠢,它是个好问题。但真正不那么酷的是,您已经编写了工作代码,而不是尝试它,而是继续让其他人运行:)我需要调用函数,如$this->function\u name
,我如何才能做到这一点?这也行得通,只需稍加修改<代码>$this->$functionName()代码>对不起!问一个愚蠢的问题,但这对我的项目来说是紧急情况。不管怎样,它工作得很好。谢谢这个问题本身并不愚蠢,它是个好问题。但真正不酷的是,您已经编写了工作代码,而不是尝试它,而是让其他人运行它:)我需要调用函数,如$this->function\u name
我该怎么做?我需要调用函数,如$this->function\u name
我该怎么做?我需要调用函数像$this->function\u name
我该怎么做?我需要调用像$this->function\u name
这样的函数我该怎么做?