Table of Content
The function_exists() function in PHP checks whether a given function is defined or not.
The function_exists() function works for both built-in and user-defined functions
PHP function_exists Syntax
PHP function_exists takes only one parameter:
function_exists(string $function_name)$function_name: The name of the function to check. Pass it as a string. It returns boolean value:
trueif the function exists.falseif it does not.
Here is a quick example:
if (function_exists('my_custom_function')) {
my_custom_function();
} else {
echo "Function not found.";
}The output:
Function not found
When you call function_exists('my_custom_function'), PHP:
- Looks for the function name in the global list of defined functions.
- Checks if the function is available (either built-in or user-defined).
- Returns
trueif it finds the function. - Returns
falseif the function is not defined yet.
So what happens behind the scenes? Let’s answer this question in the following section.
Behind the Scenes
Here is how it works:
- PHP keeps a registry of all defined functions (built-in + user-defined).
function_exists()checks that registry.- It doesn’t trigger autoloaders. So if the function is in a file that hasn’t been included yet, PHP won’t load it automatically.
So in the following example:
function play_laptop() {
echo "Opening the windows!";
}
if (function_exists('play_laptop')) {
play_laptop(); // => This runs
}Output:
Opening the windows!
The play_laptop function runs because it’s already in PHP’s global function list.
Examples of function_exists() in PHP
You can check a function name using any mix of uppercase or lowercase letters, and PHP will still find it.
For example:
function greatFLATcoding() {
echo "Hello FlatCoding Students!";
}
if (function_exists('GREATFLATCODing')) {
greatFLATcoding(); // This still runs
}Output:
Hello FlatCoding Students
You can use function_exists() to check if a function is already defined before defining it:
if (!function_exists('flatcoding_students')) {
function flatcoding_students() {
echo "Hello!";
}
}In this example, we first check if the flatcoding_students function does not exist, then we define it.
Wrapping Up
In this tutorial, you covered how the function_exists() function works in PHP and why it’s useful.
Here is a quick recap:
function_exists()checks if a function is already defined.- It works for both built-in and user-defined functions.
- It takes one string parameter—the function name—and returns
trueorfalse. - PHP looks in its global list of defined functions to check for existence.
- It doesn’t load functions from files that haven’t been included yet.
- Function names are case-insensitive when using
function_exists().
What is function_exists() in PHP?
Does function_exists() work for both built-in and user-defined functions?
Is function_exists() case-sensitive?
Similar Reads
If you are new to the PHP Spaceship Operator, then let me tell you, you're in for a treat. This…
If you want to write good PHP code, strict mode should be on your radar. Strict mode—activated by the command declare(strict_types=1); at…
In PHP, if you're working with XML files, you’ll probably work with DOMDocument. It is a powerful class, enabling you…
The Elvis operator in PHP is a shorthand version of the ternary operator. PHP allows developers to use this conditional…
PHP static property helps you manage shared data across instances, but can lead to hidden state changes. In this article,…
There are some tools known as "PHP magic constants" that will make your code a little more clever and versatile.…
When you first step into web development, one name seems to pop up everywhere: PHP. The PHP programming language has…
You can use array_filter in PHP to remove unwanted data from arrays. It works with a custom callback or default…
The PHP is_readable helps you validate file readability before any operation like read or include files. What Is PHP is_readable? The is_readable() function…
The first appearance of PHP iterable was in PHP version ( 7.1 ) – Iterables are a powerful feature in…