Change language

PHP Check if a variable is a function

To determine if a passed argument is a function or not, here are some of the preferred methods.
  • Using a function is_callable() :is a built-in function in PHP that is used to check the contents of a variable being called as a function. It can check that a simple variable contains the name of a valid function, or that an array contains a correctly encoded object and a function name. Syntax:
    bool is_callable ($var_name, $syntx_only, $calbl_name) 
    Parameters:The is_callable() function takes three parameters as shown in the above syntax and is described below. It is up to the user to use, how many parameters are one, two or three.
    • $var_name:the name of the function stored in the string variable $var_name, or an object and name method inside an object.
    • $syntx_only:if set to TRUE, the function only checks that the name can be a function or a method. It will reject simple variables that are not strings, or an array that has no valid structure to use as a callback. The valid ones should only have 2 records, the first one being an object or string and the second one - string.
    • $calbl_name:gets the name to call. This option is only available for classes.
    Return value:This function returns a boolean value. Returns TRUE if $var_name is callable, FALSE otherwise.Example.This example uses the is_callable() functionto check if a parameter is a function or not.  
    // Declare the variable and initialize // with function $function = function () { echo ’GeeksForGeeks’ ; };  
    // Function check is_callable contains
    // function or not if ( is_callable ( $function )) { echo "It is function " ; } else { echo " It is not function " ; }  
    // Declare a variable and
    // initialize this $var = "GeeksForGeeks" ; echo "" ;  
    // Function check is_callable contains
    // function or not if ( is_callable ( $var )) { echo "It is function " ; } else { echo " It is not function " ; }  
    ?>
    Exit:
    It is function It is not function 
  • instanceof:PHP’s instanceof operator is used to find out if an object is an instance of a class. Syntax:
    $f instanceof Class_Name 
    Operands:it contains two operands, which are listed below:
    • $f:is used as an object.
    • Class_Name:is used to store the class name.
    Returned value: ReturnsTrue if the object belongs to this class or has this class as one of its parents, otherwise it will return False.Example. Inthis example uses the instanceofoperator to determine if a variable is a function in PHP.  
    // Declare a variable and initialize
    // with function $func = function () { echo ’GeeksforGeeks’ ; };  
    // Use instanceof to check that 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 "" ;  
    // Use instanceof to check that it contains
    // function or not if ( $var instanceof Closure) { echo "function" ; } else { echo " not a function " ; }
    ?>
    Exit:
    function not a function 
  • Example:This example uses the methods function_exist() and is_object() to check if the argument is a function or not.  
    // Declare the function function myFun() { echo ’GeeksforGeeks’ ; };  
    // Determine if the variable is a function function is_function ( $func ) { return ( is_string ( $func ) & amp; & amp; function_exists ( $func )) || ( is_object ( $func ) & amp; & amp; ( $func instanceof Closure) ); } echo is_function ( ’myFun’ );  
    ?>
    Exit:
    1 

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method