Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Functions and Classes

PHP function_exists() function

Reference for PHP function_exists(): check whether a function is defined, case-insensitivity, namespaces, polyfills, disabled functions and related checks.

function_exists() tells you whether a function with a given name has been defined, either built into PHP, provided by a loaded extension, or declared in your code so far. It is the standard guard for optional functionality and for polyfills that define a function only if the running PHP version lacks it.

The check is about names, not about whether the function is callable from where you are: methods are not functions here, and a function that is disabled with disable_functions is reported as missing.

PHP
var_dump(function_exists('strlen'));   // bool(true)
var_dump(function_exists('nope_123')); // bool(false)

Try it

PHP Guarding optional features

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Namespaces, methods and variable functions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
function_exists(string $function): bool
Name Description
$function The function name as a string. Case-insensitive. A leading backslash is ignored, and for namespaced functions you must give the fully qualified name.

Return value: true if the function is defined, otherwise false. It never throws.

Usage notes

  • Case-insensitive. function_exists('STRLEN') is true, matching PHP's rules for calling functions.
  • Namespaces need the full name. Inside a namespace, function_exists('helper') looks for a global helper, not App\Util\helper. Use __NAMESPACE__ . '\helper' or the full string.
  • Methods are not functions. Use method_exists() for methods and is_callable() for any callable form.
  • Closures are not named functions. A variable holding a closure never makes function_exists() true; check is_callable($closure).
  • Conditional definition. Functions inside an if (!function_exists(...)) block are defined at run time, in order. Calling them before that block runs fails.
  • Top-level functions are hoisted, so function_exists('later') is true even if the declaration is further down the file.
  • Disabled functions. Functions listed in disable_functions in php.ini are treated as undefined, so function_exists('exec') is a useful safe check.
  • Extensions. To check for an extension use extension_loaded('mbstring'). To check for one function from an extension use function_exists().
  • Version checks. Prefer feature checks (function_exists) over version_compare(PHP_VERSION, ...) when a function may come from a polyfill or extension.
  • User input. Do not pass user input to function_exists() and then call the result. Whitelist names.