日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php调用字符串函数参数,如何从包含参数的变量中存储的字符串调用PHP函数

發布時間:2024/9/30 php 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php调用字符串函数参数,如何从包含参数的变量中存储的字符串调用PHP函数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我從這里找到了問題。但我需要用參數調用函數名。我需要能夠調用一個函數,但是函數名存儲在一個變量中,這是可能的嗎?例如:

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

任何幫助都會很感激。

可以使用php函數call_user_func。

function foo($argument)

{

echo $argument;

}

$functionName ="foo";

$argument ="bar";

call_user_func($functionName, $argument);

如果您在類中,可以使用call_user_func_array:

//pass as first parameter an array with the object, in this case the class itself ($this) and the function name

call_user_func_array(array($this, $functionName), array($argument1, $argument2));

我需要調用像$this->function_name這樣的函數,我該怎么做?

@薩迪卡桑,請出示我編輯過的答案。

如果要使用參數動態調用函數,可以嘗試如下操作:

function foo ($argument)

{

//code here

}

call_user_func('foo',"argument"); // php library funtion

希望對你有幫助。

我需要調用像$this->function_name這樣的函數,我該怎么做?

哇,一個擁有4枚金牌的用戶不會有這樣的問題。你的代碼已經工作了

function foo ($argument)

{

echo $argument;

}

function bar ($argument)

{

//code here

}

$functionName ="foo";

$argument="Joke";

$functionName($argument); // works already, might as well have tried :)

?>

產量

Joke

小提琴

從理論上講,這種函數叫做變量函數

PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.

我需要調用像$this->function_name這樣的函數,我該怎么做?

這也可以,只要稍加修改。$this->$functionName();

對不起的!問個愚蠢的問題,但這對我的項目來說很緊急。不管怎樣,它工作得很好。謝謝

這個問題本身并不愚蠢,是個好問題。但真正不酷的是,你已經編寫了可工作的代碼,而不是嘗試它,而是繼續讓其他人運行它:)

總結

以上是生活随笔為你收集整理的php调用字符串函数参数,如何从包含参数的变量中存储的字符串调用PHP函数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。