381 of 410 menu

The is_callable Function

The is_callable function checks if the passed value is callable. The first parameter accepts the value to check, the second parameter (optional) is a flag for syntactic name checking, and the third (optional) is a string to store the callable name.

Syntax

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

Example

Let's check a regular function:

<?php function test() {} $res = is_callable('test'); var_dump($res); ?>

Code execution result:

true

Example

Let's check a class method:

<?php class MyClass { public function method() {} } $obj = new MyClass(); $res = is_callable([$obj, 'method']); var_dump($res); ?>

Code execution result:

true

Example

Let's check a non-existent function:

<?php $res = is_callable('non_existent_function'); var_dump($res); ?>

Code execution result:

false

Example

Using the third parameter to get the name:

<?php function myFunction() {} $name = ''; $res = is_callable('myFunction', false, $name); echo $name; ?>

Code execution result:

'myFunction'

See Also

  • the function_exists function,
    which checks for the existence of a function
byenru