PHP | Check if a variable is a function Last Updated : 05 Mar, 2019 Comments Improve Suggest changes Like Article Like Report To determine whether a passed argument is a function or not, Few of the most preferred methods are shown below. Using is_callable() Function: It is an inbuilt function in PHP which is used to verify the contents of a variable in called as a function. It can check that a simple variable contains the name of a valid function, or that an array contains a properly encoded object and function name. Syntax: bool is_callable ( $var_name, $syntx_only, $calbl_name ) Parameters: The is_callable() function accepts three parameters as shown in above syntax and are described below. It depends on user to use how many parameters one, two or three. $var_name: The name of a function stored in a string variable $var_name, or an object and the name of a method within the object. $syntx_only: If it set to TRUE the function only verifies that name might be a function or method. It will 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. $calbl_name: It receives the callable name. This option only implemented for classes. Return Value: This function returns a boolean type value. It returns TRUE if $var_name is callable, FALSE otherwise. Example: This example uses is_callable() function to verify whether the parameter is a function or not. php <?php // Declare a variable and initialize // with function $function = function() { echo 'GeeksForGeeks'; }; // Check is_callable function contains // function or not if( is_callable( $function ) ) { echo "It is function"; } else { echo "It is not function"; } // Declare a variable and // initialize it $var = "GeeksForGeeks"; echo "\n"; // Check is_callable function contains // function or not if( is_callable( $var ) ) { echo "It is function"; } else { echo "It is not function"; } ?> Output: It is function It is not function instanceof: The instanceof operator in PHP is used to find out if an object is an instantiated instance of a class. Syntax: $f instanceof Class_Name Operands: It contains two operands which are listed below: $f: It is used as an object. Class_Name: It is used to hold the class name. Return Value: It returns True if the object is of this class or has this class as one of its parents else it will return a False value. Example: This example use instanceof operator to determine if a variable is a function in PHP. php <?php // Declare a variable and initialize // with function $func = function() { echo 'GeeksforGeeks'; }; // Use instanceof to check it contains // function or not if($func instanceof Closure) { echo "function"; } else { echo "not a function"; } // Declare a variable and initialize it $var = "GFG"; echo "\n"; // Use instanceof to check it contains // function or not if($var instanceof Closure) { echo "function"; } else{ echo "not a function"; } ?> Output: function not a function Example: This example uses function_exist() and is_object() methods to check whether an argument is a function or not. php <?php // Declare a function function myFun() { echo 'GeeksforGeeks'; }; // Determine if a variable is a function function is_function($func) { return (is_string($func) && function_exists($func)) || (is_object($func) && ($func instanceof Closure)); } echo is_function('myFun'); ?> Output: 1 Comment More infoAdvertise with us Next Article PHP | Check if a variable is a function P PranchalKatiyar Follow Improve Article Tags : Web Technologies PHP PHP Programs PHP-function Similar Reads How to check Whether a Variable is Null in PHP? We are given a variable and the task is to check whether the value of the given variable is null or not and returns a Boolean value using PHP. To check a variable is null or not, we use is_null() function. A variable is considered to be NULL if it does not store any value. It returns TRUE if the val 1 min read PHP trait_exists() Function PHP implements a way to reuse code called Traits. The trait_exists() function in PHP is an inbuilt function that is used to check whether a trait exists or not. This function accepts the traitname and autoload as parameters and returns true if trait exists, false if not, null in case of an error. Sy 2 min read How to check whether a variable is set or not using PHP ? We have given a variable and the task is to check whether a variable var is set or not in PHP. In order to do this task, we have the following methods in PHP: Approach 1: Using isset() Method: The isset() method returns True if the variable is declared and its value is not equal to NULL. Syntax: boo 2 min read What is the difference between is_a() function and instanceof in PHP? is_a() Function The is_a() is a built-in function in PHP which is used to check whether a given object is of a given class or not. It also checks if the given class is one of the parents of the given object or not. Syntax: bool is_a( $object, $class_name, $allow_string ) Parameters: This function ac 3 min read How to check whether an array is empty using PHP? In PHP, arrays are commonly used data structures that can hold multiple values. However, sometimes it is necessary to check whether an array is empty, i.e., whether it has no elements. This is useful when the array is filled with data automatically, and you need to check if it has any data before pe 3 min read How to check if URL contain certain string using PHP? Given a URL and the task is to check the URL contains certain string or not. The URL are basically the strings. So in order to check the existence of certain strings, two approaches can be followed. The first approach is used to find the sub string matching in a string and second approach is to find 4 min read How to find out where a function is defined using PHP ? When we do projects then it includes multiple modules where each module divided into multiple files and each file containing many lines of code. So when we declare a function somewhere in the files and forget that what the function was doing or want to change the code of that function but can't find 3 min read Difference between isset() and array_key_exists() Function in PHP isset() function The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases. Syntax: boo 2 min read How to check the existence of URL in PHP? Existence of an URL can be checked by checking the status code in the response header. The status code 200 is Standard response for successful HTTP requests and status code 404 means URL doesn't exist. Used Functions: get_headers() Function: It fetches all the headers sent by the server in response 2 min read How to determine if an array contains a specific value in PHP? Determining if an array contains a specific value in PHP involves verifying whether a particular element exists within an array. This task is essential for various programming scenarios, such as searching for user input, validating data, or filtering results based on specific criteria. PHP offers se 2 min read Like