Use function_exists() when you need to checks whether a function has been defined and is callable by name in a PHP application. The examples below show the actual operation and its limitations.
What Is function_exists() in PHP?
Checks whether a function has been defined and is callable by name. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
function_exists(string $function): bool
Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.
Example 1: Basic Usage
<?php
var_dump(function_exists("strlen"));
Output:
bool(true)
Example 2: Different Input or Behavior
<?php
var_dump(function_exists("not_a_real_function_xyz"));
Output:
bool(false)
Practical Example: Applying the Function
<?php
$optionalFeatures = ["mb_strlen", "iconv_strlen"]; foreach ($optionalFeatures as $feature) echo $feature . ": " . (function_exists($feature) ? "available" : "missing") . PHP_EOL;
Output:
mb_strlen: missing
iconv_strlen: available
Important Behavior and Edge Cases
It does not prove that an extension is enabled for a particular operation or that a call will succeed.
Comparison With Related Functions
| Approach | Difference |
|---|---|
function_exists() | Checks whether a function has been defined and is callable by name. |
| Related operation | is_callable() also checks callable syntax and methods; function_exists() checks functions by name. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | It does not prove that an extension is enabled for a particular operation or that a call will succeed. |
| Confusing this function with a related operation | is_callable() also checks callable syntax and methods; function_exists() checks functions by name. |
Key Takeaways
- Checks whether a function has been defined and is callable by name.
- It does not prove that an extension is enabled for a particular operation or that a call will succeed.
- is_callable() also checks callable syntax and methods; function_exists() checks functions by name.
Use function_exists() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.