W3docs

is_callable()

The is_callable() function is a built-in function in PHP that checks whether a variable is a valid callable function or method. A callable function or method is

Introduction

The is_callable() function checks whether a value can be called as a function — and returns true or false accordingly. A value is callable if PHP can invoke it: a built-in or user-defined function name, a method, a closure, or an object that implements __invoke().

This page covers what counts as callable, the function's three parameters (including the often-overlooked $syntax_only and $callable_name), the many forms a callable can take, and the gotchas that catch people out. The typical reason to reach for is_callable() is to guard a call — verify a value is invocable before you actually invoke it, so you fail gracefully instead of with a fatal error.

Syntax

is_callable(mixed $value, bool $syntax_only = false, string &$callable_name = null): bool
ParameterDescription
$valueThe value to test. May be a string (function name), an array [object, 'method'] or ['Class', 'staticMethod'], a Closure, or an invokable object.
$syntax_onlyIf true, only checks that $value looks like a valid callable (a string, or a 2-element array of the right shape) without verifying the function/method actually exists. Default false does the full check.
&$callable_namePassed by reference. After the call it receives the resolved name, e.g. "strlen" or "TestClass::testMethod".

It returns a bool: true if $value is callable, false otherwise.

Basic example

The four most common callable forms — a function name, a method on an instance, a static method, and a non-callable string:

<?php
function testFunction()
{
    echo "Hello world!";
}

class TestClass
{
    public function testMethod() {}
    public static function staticMethod() {}
}

$var1 = "testFunction";                       // function name
$var2 = [new TestClass(), "testMethod"];      // [object, method]
$var3 = ["TestClass", "staticMethod"];        // [class, static method]
$var4 = "not_a_callable";                     // nothing by this name

var_dump(is_callable($var1)); // bool(true)
var_dump(is_callable($var2)); // bool(true)
var_dump(is_callable($var3)); // bool(true)
var_dump(is_callable($var4)); // bool(false)
?>

We use var_dump() here instead of echo because echo-ing a boolean prints 1 for true and an empty string for false — which is easy to misread. var_dump() shows the type explicitly.

Closures and invokable objects

A Closure (anonymous function) is always callable. So is any object whose class defines the magic __invoke() method — those objects can be used with $obj() syntax:

<?php
$closure = function () { return "called"; };

class Multiplier
{
    public function __invoke($n) { return $n * 2; }
}

var_dump(is_callable($closure));            // bool(true)
var_dump(is_callable(new Multiplier()));    // bool(true)
var_dump(is_callable("strlen"));            // bool(true) — built-in functions count too
?>

Guarding a call before you make it

The main practical use: check first, then call, so a bad value never triggers a fatal error.

<?php
function runIfPossible($maybeCallback)
{
    if (is_callable($maybeCallback)) {
        return $maybeCallback();
    }
    return "Nothing to run.";
}

echo runIfPossible(fn() => "It ran!") . "\n";   // It ran!
echo runIfPossible("missing_function") . "\n";  // Nothing to run.
?>

$syntax_only: shape vs. existence

With $syntax_only = true, is_callable() only checks that the value has the shape of a callable — it does not confirm the target exists. This is faster but weaker:

<?php
// "ghost" is not a real function:
var_dump(is_callable("ghost"));        // bool(false) — full check, fails
var_dump(is_callable("ghost", true));  // bool(true)  — syntax only, just "is a string"
?>

Use the default (false) when you intend to actually call the value. Reserve true for cases where the target will be defined later (e.g. registering callbacks before their functions load).

$callable_name: getting the resolved name

The third parameter is filled by reference with the canonical name of the callable — handy for logging or error messages:

<?php
class Greeter
{
    public function hello() {}
}

is_callable([new Greeter(), "hello"], false, $name);
echo $name . "\n";   // Greeter::hello

is_callable("trim", false, $name2);
echo $name2 . "\n";  // trim
?>

Common gotchas

  • echo hides booleans. echo is_callable($x) prints 1 for true and nothing for false. Prefer var_dump() or an if.
  • Private/protected methods are not callable from outside. is_callable() respects visibility — a [object, 'privateMethod'] pair returns false when checked from outside the class scope.
  • is_callable() is not function_exists(). function_exists() only accepts a string function name; is_callable() accepts every callable form (closures, method arrays, invokable objects) — use it when the value could be any of them.
  • First-class callable syntax (PHP 8.1+). strlen(...) produces a Closure, which is_callable() reports as true.

Conclusion

is_callable() answers one question reliably: can I call this value? It recognizes function names, method arrays, static methods, closures, and __invoke() objects, while respecting visibility rules. Use it to guard calls to dynamic callbacks, use $callable_name to recover a readable name for logs, and reach for $syntax_only only in the narrow case where the target is defined later.

Practice

Practice
What is the main purpose of is_callable() function in PHP?
What is the main purpose of is_callable() function in PHP?
Was this page helpful?