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

phpfunction_exists(string $function_name): bool

Example

Let's check the existence of the standard function strlen:

php<?php
  $res = function_exists('strlen');
  var_dump($res);
?>

Code execution result:

phptrue

Example

Let's check the existence of a non-existent function:

php<?php
  $res = function_exists('nonexistent_function');
  var_dump($res);
?>

Code execution result:

phpfalse

Example

Let's check the function existence before and after its definition:

php<?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:

phpfalse
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