• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Using function_exists() to Check if Function Exists in php

Using function_exists() to Check if Function Exists in php

March 18, 2022 Leave a Comment

In php, we can check if a function exists with the php function_exists() function.

if (function_exists('max')) {
    echo "max() exists!";
} else {
   echo "max() doesn't exist!";
}

if (function_exists('other_function')) {
    echo "other_function() exists!";
} else {
   echo "other_function() doesn't exist!";
}

//Output:
max() exists!
other_function() doesn't exist!

When working in php, it is useful to be able to check if a function exists or not.

To check if a function exists in our php program, we can use the function_exists() function. function_exists() takes a function name in the form of a string and checks to see if the function exists.

If the function exists, function_exists return true. Otherwise, function_exists returns false.

function_exists() can be useful so that we don’t try to call a function which hasn’t been defined and get an error.

Below are some examples of testing to see if a function exists in php.

if (function_exists('max')) {
    echo "max() exists!";
} else {
   echo "max() doesn't exist!";
}

if (function_exists('other_function')) {
    echo "other_function() exists!";
} else {
   echo "other_function() doesn't exist!";
}

//Output:
max() exists!
other_function() doesn't exist!

How to Check if a User Defined Function Exists in php with function_exists()

We can use the function_exists() function to check if a user defined function exists in our php code.

Below are some examples of checking for the existence of user defined functions in php with function_exists().

function exampleFunction($a) {
    return "What's up?";
}

if (function_exists('exampleFunction')) {
    echo "exampleFunction() exists!";
} else {
    echo "exampleFunction() doesn't exist!";
    function anotherFunction() {
        return "Not much.";
    }
}

if (function_exists('anotherFunction')) {
    echo "anotherFunction() exists!";
} else {
    echo "anotherFunction() doesn't exist!";
}

// Output:
exampleFunction() exists!
anotherFunction() doesn't exist!

Hopefully this article has been useful for you to learn how to check if a function exists or not in php.

Other Articles You'll Also Like:

  • 1.  php str_replace – Replace String in php
  • 2.  Delete Cookie Using php
  • 3.  php atan2 – Find Arctangent of the Quotient of Two Numbers
  • 4.  How to Get Current System IP Address in php
  • 5.  php Random Number Generator with rand Function
  • 6.  php in_array – Check If Value is in php Array
  • 7.  php array_flip() – Flip the Keys and Values of an Array in php
  • 8.  php acosh – Find Hyperbolic Arccosine of Number Using acosh() Function
  • 9.  php getimagesize – Get Height, Width and Image File Type
  • 10.  Get Query String from URL in php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy