Checking whether a function is available

bool function_exists ( string function_name)

If you're working with functions that are not part of the core of PHP, that is, functions that are from an extension that needs to be enabled by users, it's a smart move to use the function_exists() function. This takes a function name as its only parameter, and returns true if that function (either built-in, or one you've defined yourself) is available for use. Note that it only checks whether the function is available, not whether it will work - your system may not be configured properly for some functions.

Author's Note: If you ever want to know whether you have a function available to you, use the function_exists() function. This takes one string parameter that is the name of a function, and returns true if the function exists or false if it does not. Many people use function_exists() to find out whether they have an extension available, by calling function_exists() on a function of that extension. However, this is accomplished much more easily with the function extension_loaded() function covered later.

 

Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Extension functions >>

Previous chapter: A regular expression assistant

Jump to:

 

Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.