What is the function_exists method in PHP?
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.
Syntax
function_exists(string $functionName): bool
Returns
This method returns true if the function is defined. Otherwise it returns false.
Code
<?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'));?>
Explanation
In the code above,
-
We have created two functions
testandtest2. -
We have used the
function_existsmethod to check if the function is defined. -
This method returns
truefor the functionstest,test2, andstrlen(built-in function). -
This method returns
falsefor thetempfunction name, because there is no method available with the nametemp.