i

PHP Tutorial

Testing For A Function Existence

A function present in php checks the existence of another function. function_exists is the function that checks whether a function is available in php or not. It gives true value if the function is present in php. 

function_exists check the file functions.php if the function is present in some other php file and we are trying to call in our current file. If we do not check function_exists and our function is present in any other file it will throw an error at runtime mentioning Call to undefined function.

It prevents php fatal error for a webpage and allows you to choose the other set of code that should run when the file is deleted or not present in functions.php.

You can use function_exists as below.

if (function_exists(‘greeting’))

{

           greeting();

}

else

{

           echo “Sorry, the function is missing”;

}

?>

So, if in any other file, greeting function exists, the function is called otherwise the else statement will run and mention the function is missing instead of a fatal error in php.