PHP Function Reference

PHP is_callable() Function



The PHP is_callable() function checks whether the content of a variable can be called as a function from the current scope or not. The function returns true if the content of the variable can be called, else returns false.

Syntax

is_callable(variable, syntax_only, callable_name)

Parameters

variable Required. Specify the variable being evaluated.
syntax_only Optional. If set to true the function only verifies that variable 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. Default is false.
callable_name Optional. Receives the "callable name" (only for classes).

Return Value

Returns true if variable is callable, false otherwise.

Example: is_callable() example

The example below shows the usage of is_callable() function.

<?php
//declaring a function
function func_test() {
}

$func_name = 'func_test';

var_dump(is_callable("func_test"));
var_dump(is_callable($func_name));
var_dump(is_callable($func_name, false, $callable_name));
echo $callable_name, "\n";
?>

The output of the above code will be:

bool(true)
bool(true)
bool(true)
func_test

Example: array containing a method

Consider the example below, an array containing method is used with this function.

<?php
//declaring a function
class someClass {
  function someMethod() {
  }
}

//creating an object instance
$obj = new someClass();

$var_name = array($obj, 'someMethod');

var_dump(is_callable($var_name));
var_dump(is_callable($var_name, false, $callable_name));
echo $callable_name, "\n";
?>

The output of the above code will be:

bool(true)
bool(true)
someClass::someMethod

❮ PHP Variable Handling Reference