The function_exists Function
The function_exists
function checks if the specified function has been defined.
It returns true
if the function exists, and false
otherwise.
The function accepts one parameter - the name of the function to check as a string.
Syntax
function_exists(string $function_name): bool
Example
Let's check the existence of the standard function strlen
:
<?php
$res = function_exists('strlen');
var_dump($res);
?>
Code execution result:
true
Example
Let's check the existence of a non-existent function:
<?php
$res = function_exists('nonexistent_function');
var_dump($res);
?>
Code execution result:
false
Example
Let's check the function existence before and after its definition:
<?php
$res1 = function_exists('custom_function');
var_dump($res1);
function custom_function() {
return 'Hello';
}
$res2 = function_exists('custom_function');
var_dump($res2);
?>
Code execution result:
false
true
See Also
-
the
method_exists
function,
which checks for the existence of a class method -
the
is_callable
function,
which checks if a value can be called as a function