function_exists
Checks whether a function is defined.
Syntax
function_exists(string $function): boolParameters
| Parameter | Type | Description |
|---|---|---|
| $function | string | The function name. |
Return value
bool`true` if the function exists.
Examples
A built-in and a missing function:
<?php
var_dump(function_exists('strlen'));
var_dump(function_exists('no_such_function'));Output:
bool(true)
bool(false)Checking that an extension is available:
<?php
echo function_exists('mb_strlen') ? 'mbstring is available' : 'mbstring is missing';Output:
mbstring is availableNotes
This is how you check that a PHP extension is installed before using its functions.
See also
- method_exists — Checks whether a class or an object has a method.
- class_exists — Checks whether a class is defined.
- call_user_func — Calls a function held in a variable.
Related recipes
In the tutorial
- Anonymous functions and closures — Functions as values in PHP — anonymous and arrow functions, the use construct, the callable type.