Stack Overflow
Stack Overflow

Reputation: 2968

What are 2 parameters do in is_callable() function in php?

I am new to php can anyone tell what the last 2 parameters do in is_callable() function in php ?

In the following examples it returns same result with true and false

First with false

function hello(){
    return "Hello";
}

$x = 'hello';
echo is_callable($x,false);

Return true

Now with true

function hello(){
    return "Hello";
}

$x = 'hello';
echo is_callable($x,true);

Returns true

What basically the last two parameters are used for in this function.

is_callable(parOne,parTwo,parThree);

I have read about this on php.net but failed to understand. Can anyone tell for what the last two parameters are used in this function ?

Upvotes: 2

Views: 497

Answers (2)

Agnius Vasiliauskas
Agnius Vasiliauskas

Reputation: 11277

echo '<pre>';
print_r(
        ["is_callable(null, true)" => [(int)is_callable(null, true), "because null can't store callable name"],
        "is_callable(7,    true)"  =>  [(int)is_callable(7,    true), "because integer can't store callable name"],
        "is_callable('so', true)"  => [(int)is_callable('so', true), "because callable name can be saved in string"],

         "is_callable('so', false)"  => [(int)is_callable('so', false), "because such callable doesn't exists in code"],
         "is_callable('is_callable', false)"  => [(int)is_callable('is_callable', false), "because such callable exists and can be executed"],
        ]
        );
echo '</pre>';

So in short - parameter $syntax_only = true just does type checking - checks if callable name CAN be stored in input variable. While false - additionally checks if such callable really exists in code and can be executed.

Upvotes: 1

Barmar
Barmar

Reputation: 781058

When the second argument is true, it doesn't check whether the first argument actually names an existing function, just whether it has the appropriate syntax to be used to try to call a function. That means it's either a string, or an array whose first element is an object and second element is a string.

For instance:

is_callable('hellox', false);

returns FALSE because there's no hellox() function, but:

is_callable('hellox', true);

returns TRUE because it could be the name of a function.

But

is_callable(1.23, true);

returns FALSE because a number can't be used as a function.

Upvotes: 2

Related Questions