PHP - is_callable() 函式



定義和用法

is_callable() 函式檢查變數的內容是否可以作為函式呼叫。

語法

bool is_callable ( mixed $value , bool $syntax_only = false , string &$callable_name = null )

引數

序號 引數及描述
1

必填。要驗證的值。

2

syntax_only

可選。如果設定為true,則函式僅驗證值可能是函式或方法。它只會拒絕不是字串的簡單變數,或者結構無效無法用作回撥的陣列。有效的回撥陣列應該只有兩項,第一項是物件或字串,第二項是字串。

3

callable_name

可選。返回一個可呼叫名稱(僅限於類)。

返回值

此函式返回布林型值。如果可呼叫,則返回true,否則返回false

依賴

PHP 4.0.6 及以上版本

示例

以下示例演示如何檢查變數是否可以作為函式呼叫:

<?php
   function testFunction(){
   }
   $functionVariable = 'testFunction';
   var_dump(is_callable($functionVariable, false, $callable_name));   // bool(true)
   echo $callable_name. "<br>";   // testFunction
   // using only-one parameter
   var_dump(is_callable($functionVariable));
?>

輸出

這將產生以下結果:

bool(true) testFunction
bool(true)
php_variable_handling_functions.htm
廣告