PHP function_exists: Avoid Function Redeclaration

PHP function exists

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:

  • true if the function exists.
  • false if 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:

  1. Looks for the function name in the global list of defined functions.
  2. Checks if the function is available (either built-in or user-defined).
  3. Returns true if it finds the function.
  4. Returns false if 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 true or false.
  • 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?

The function_exists() function in PHP checks whether a given function is defined or not. It takes one string parameter—the function name—and returns true if the function exists, or false if it does not.

Does function_exists() work for both built-in and user-defined functions?

Yes. The function_exists() function works for both built-in and user-defined functions. PHP keeps a registry of all defined functions, and function_exists() checks that registry to determine if a function exists.

Is function_exists() case-sensitive?

No. Function names are case-insensitive when using function_exists(). You can use any mix of uppercase or lowercase letters, and PHP will still recognize the function.

Similar Reads

PHP strlen Function: How It Works with Strings

Developers needed a way to check the length of a string, so the strlen function was introduced to handle this…

PHP OR Operator: Using || and or in Conditional Statements

The PHP OR operator has two renditions: either || or or. Both of these are useful logical operators when you want to introduce…

PHP Conditional Operator: How It Works with Examples

The PHP shorthand conditional operator gives a quick way to choose between two values and replaces long if-else blocks. What…

PHP MySQL LIMIT Data: How to Optimize PHP Queries?

There are situations where you only need a specific number of rows returned. This is where the "LIMIT" clause in…

PHP mb_strtolower: How to Handle Multibyte Strings Properly

The mb_strtolower() function in PHP turns every letter in a string into lowercase. It works with multibyte encodings like UTF-8.…

Concatenating Strings in PHP: Tips and Examples

In PHP, string operators, such as the concatenation operator (.) and its assignment variant (.=), are employed for manipulating and…

PHP Static Property: How It Works & Examples

PHP static property helps you manage shared data across instances, but can lead to hidden state changes. In this article,…

PHP array_chunk(): Split Arrays into Manageable Parts

When working with big arrays in PHP, things can get messy. That is where php array_chunk() steps in by providing…

Class and Object in PHP: How They Work with a Real Example

Classes and objects are important parts of OOP in PHP. They help you organize your code so you can update…

PHP Variable Scope: Local, Global & Static

The variable scope in PHP refers to the variables, functions, and classes that can be accessed within different parts of…

Previous Article

PHP error_log Function: How It Works with Examples

Next Article

PHP Escape Characters: How to Escape Special Characters

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.