Skip to content
Related Articles

Related Articles

PHP | Check if a variable is a function
  • Last Updated : 05 Mar, 2019

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
      
    // 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
      
    // 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
      
    // 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
    
My Personal Notes arrow_drop_up
Recommended Articles
Page :