is_callable

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

Syntax

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

Parameters

value

The value to check

syntax_only

If set to true the function only verifies that value 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 value is callable, false otherwise.

Examples

1 · value · Function

<?

function myfunction()
{
}

$value = "myfunction";

$return = is_callable($value);

var_export($return);

?>
true

2 · value · Method

<?

class myclass
{
    function myfunction()
    {
    }
}

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

$return = is_callable($value);

var_export($return);

?>
true

3 · syntax_only · Function

<?

function myfunction()
{
}

$value = "myfunction";
$syntax_only = true;

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

var_export($return);

?>
true

4 · syntax_only · Method

<?

class myclass
{
    function myfunction()
    {
    }
}

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

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

var_export($return);

?>
true

5 · callable_name · Function

<?

function myfunction()
{
}

$value = "myfunction";
$syntax_only = true;

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

var_export($return);
echo PHP_EOL . $callable_name;

?>
true
myfunction

6 · callable_name · Method

<?

class myclass
{
    function myfunction()
    {
    }
}

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

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

var_export($return);
echo PHP_EOL . $callable_name;

?>
true
myclass::myfunction

boolval

debug_zval_dump

doubleval

empty

floatval

get_debug_type

get_defined_vars

get_resource_id

get_resource_type

gettype

intval

is_array

is_bool

is_countable

is_double

is_float

is_int

is_integer

is_iterable

is_long

is_null

is_numeric

is_object

is_real

is_resource

is_scalar

is_string

isset

print_r

serialize

settype

strval

unserialize

unset

var_dump

var_export

PHP

Home Menu