is_callable

Verify that the contents of a variable can be called as a function

Syntax

is_callable ( mixed $var [, bool $syntax_only = FALSE [, string &$callable_name ]] ) : bool

Parameters

var

The value to check

syntax_only

If set to TRUE the function only verifies that var might be a function or method. It will only reject simple variables that are not strings, or an array that does not have a valid structure to be used as a callback. The valid ones are supposed to have only 2 entries, the first of which is an object or a string, and the second a string.

callable_name

Receives the "callable name".

Return

Returns TRUE if var is callable, FALSE otherwise.

Examples

var | Function

<?

function myfunction() {}

$var = "myfunction";

$return = is_callable($var);

echo $return;

?>
1

var | Method

<?

class myclass {
    function myfunction() {}
}

$var = array(new myclass, "myfunction");

$return = is_callable($var);

echo $return;

?>
1

syntax_only | Function

<?

function myfunction() {}

$var = "myfunction";
$syntax_only = true;

$return = is_callable($var, $syntax_only);

echo $return;

?>
1

syntax_only | Method

<?

class myclass {
    function myfunction() {}
}

$var = array(new myclass, "myfunction");
$syntax_only = true;

$return = is_callable($var, $syntax_only);

echo $return;

?>
1

callable_name | Function

<?

function myfunction() {}

$var = "myfunction";
$syntax_only = true;

$return = is_callable($var, $syntax_only, $callable_name);

echo $return . PHP_EOL;
echo $callable_name;

?>
1
myfunction

callable_name | Method

<?

class myclass {
    function myfunction() {}
}

$var = array(new myclass, "myfunction");
$syntax_only = true;

$return = is_callable($var, $syntax_only, $callable_name);

echo $return . PHP_EOL;
echo $callable_name;

?>
1
myclass::myfunction

PHP

Home Menu