如何呼叫儲存在變數中的字串中的函式 PHP?


要從儲存在變數中的字串中呼叫函式,請使用 $func。

語法如下 

$func=’anyFunctionName’;

示例

PHP 程式碼如下 

 線上演示

<!DOCTYPE html>
<html>
<body>
<?php
function hello(){
   echo "Calling hello method."."<br>";
}
function welcome(){
   echo "Calling welcome method."."<br>";
}
$func = 'hello';
$func();
?>
</body>
</html>

輸出

將會產生以下輸出

Calling hello method.

更新於: 13-10-2020

1 千 + 檢視

開啟你的 職業生涯

透過完成課程取得認證

開始
廣告
© . All rights reserved.