PHP Function exists


PHP function_exists() function is used to detect whether the function is defined, the function can be detected PHP built-in function, the user can also be a custom function. returns TRUE if the function being detected exists, otherwise returns FALSE.

grammar:

bool function_exists(string function_name)

Example 1, the detection of user-defined function:

function testfunc() {
    echo 'I am a custom function';
}
if (! function_exists('testfunc')) {
    function testfunc() {
        echo 'I am a custom function';
    }
}
testfunc();

Example 2, detection system built-in function, the following code is common to check the system is open GD library:

if (function_exists('gd_info')) {
    echo 'The GD library is already open. ';
} else {
    echo 'The GD library is not open. ';
}

Special circumstances

The function_exists() function has a special case where function_exists() returns directly to the original function value when the argument is not passed as a function name, but as function_name().

function testfunc() {
    echo 'I am a custom function';
}
echo function_exists(testfunc());

Run the example output:

I'm a custom function



Copyright 2014-2017 by DeveloperTut.com. All Rights Reserved.
TwitterGoogle+Facebook