Hudzilla.org - the homepage of Paul Hudson
Contents > Functions Wish List | Report Bug | About Me ]

4.9     Checking whether a function is available: function_exists()

This is NOT the latest copy of this book; click here for the latest version.

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.





<< 4.8.7 The regular expressions coach   4.10 Extension functions: get_loaded_extensions(), get_extension_funcs(), dl(), and extension_loaded() >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
deathgod - 08 Sep 2008

I have read over and over people asking the same damn thing on every single page. If you want an example when Rock Hudson thinks its not necessary then write it yourself and post it here. For Example,:) here is an example relavant to this page:

<?php
$var = function_exists(tep_my_function());

if ($var=1) echo "Wooh dude, cool function";
else echo "Dude, where\'s my function?";
?>

DoctoR JackaL - 08 Sep 2008

It would be best if you give some examples , and showing the outputs if TRUE or FALSE , thx , great book !



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is one plus two?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow