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

<?php
function 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 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.

Free Resources