The function_exists()
method can be used to check if the function is defined or not. This will check both the user-defined and built-in functions.
function_exists(string $functionName): bool
This method returns true
if the function is defined. Otherwise it returns false
.
<?phpfunction test(){}function test2(){}echo "Is test function exists: ";var_dump(function_exists('test'));echo "\nIs test2 function exists: ";var_dump(function_exists('test2'));echo "\nIs strlen built-in function exists: ";var_dump(function_exists('strlen'));echo "\nIs temp function exists: ";var_dump(function_exists('temp'));?>
In the code above,
We have created two functions test
and test2
.
We have used the function_exists
method to check if the function is defined.
This method returns true
for the functions test
, test2
, and strlen
(built-in function).
This method returns false
for the temp
function name, because there is no method available with the name temp
.