Check the Existence of a Function

Over the last couple of months I have written numerous tutorial posts for WordPress platform. In many instances, I have shown how to write certain function and call them whenever or wherever you need them. I am also going to write tutorials on PHP as well on upcoming days.

Today I will not write any custom function rather I will talk about a simple practice that would help you to be a smart coder at some point down the line. We will draw our attention to a small PHP function called function_exists. This function is one of many functions in PHP known as “Function Handling Function” or in other words, these functions deals with the existing (built-in) or custom written and declared functions.

How to Use?

Take a look at the little demonstration at the bottom. This is how this function can be used.

<?php
if (function_exists('function_name')) {
   // IF is TRUE: Perform this operation
}
?>

On second line all we are doing is to check if function_name() has already been defined or not. It returns a TRUE or FALSE value. If the specified is found on the defined functions list, it definitely should return a TRUE value. Only then the inner operation within the curly braces ({ }) would be performed.

However, you can always get creative with your snippet and define if it returns a FALSE value.

<?php
if (function_exists('function_name')) {
   // IF is TRUE: Perform this operation
} else {
   // IF is FALSE: Perform this operation
}
?>

Purpose

The ultimate goal of this function is the check the availability of a possibly declared functions before you can actually call them. If you normally call a function within the page that has not been declared already, your site mostly going to break down. To prevent such break-down, it is highly recommended that you always check your function with function_exists. This procedure can also work as a fail-safe mechanism.

Such function can be extremely useful if you are writing plugin of custom function for CMS platforms like WordPress or others which is dependent on native CMS functions. As we have witnesses in the past that many built-in functions of WordPress has been deprecated over the years. Here is a little demonstration of such function for your understanding.

<?php
if (function_exists('is_post')) {
   // call the your function
   echo your_custom_function(); }
?>

In this section, we are checking if “is_post” function from WordPress exists or not. If it does then it would run “your_custom_function”. Now, I already know that “is_post” function has been deprecated from WordPress and has been replaced it by “is_single” function. So, very normally my custom function won’t be called as “is_post” function doesn’t exist and potentially saving my site from breaking down.

If you are writing plugin that would be widely distributed should have such fail-safe mechanism. Because there’s always a chance that WordPress might drop a function on it’s new version that you may have used on your plugin. I think you got the idea.

Final Words

Whether you are writing your own PHP functions for you custom PHP based site or any other PHP based platform, its always recommended that you call them through “function_exists” function. This mechanism is highly effective and even more important while you are writing functions for well known platform like WordPress or others. I hope this little tips will help our new PHP developers.

Today In History

  •  
  •  

Comment

Leave a Reply

Note: Convet HTML, PHP, JavaScripts from HTMLify, before posting from comment section.
License: By submitting a comment here you grant this site a perpetual license to reproduce your words and name/Web site in attribution. Please use your real name or a pseudonym (i.e., pen name, alias, nom de plume) when commenting. If you add your site name, company name, or something completely random, I'll likely change it to whatever I want.