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

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

Example

Let's check a regular function:

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

Code execution result:

phptrue

Example

Let's check a class method:

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

Code execution result:

phptrue

Example

Let's check a non-existent function:

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

Code execution result:

phpfalse

Example

Using the third parameter to get the name:

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

Code execution result:

php'myFunction'

See Also

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