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.
var_dump(function_exists('strlen')); // bool(true) var_dump(function_exists('nope_123')); // bool(false)
Try it
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.
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
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')istrue, matching PHP's rules for calling functions. - Namespaces need the full name. Inside a namespace,
function_exists('helper')looks for a globalhelper, notApp\Util\helper. Use__NAMESPACE__ . '\helper'or the full string. - Methods are not functions. Use
method_exists()for methods andis_callable()for any callable form. - Closures are not named functions. A variable holding a closure never makes
function_exists()true; checkis_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_functionsinphp.iniare treated as undefined, sofunction_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 usefunction_exists(). - Version checks. Prefer feature checks (
function_exists) overversion_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.
Related
call_user_func(), Callbacks, Defining functions,isset(), Namespaces- Lessons: Functions, Namespaces