Testing for the Existence of a Function

You have seen that we do not always know that a function exists before we try to invoke it. If our code were to work with a function name stored in a variable, for example, it would be useful to be able to test whether or not the function exists before we attempt to call it. Furthermore, different builds of the PHP engine may include different functionality. If you are writing a script that may be run on multiple servers, you might want to verify that key features are available. You might write code that will use MySQL if MySQL-related functions are available, but simply log data to a text file otherwise.

You can use function_exists() to check for the availability of a function. function_exists() requires a string representing a function name. It will return true if the function can be located and false otherwise.

Listing 6.16 shows function_exists() in action, and illustrates some of the other topics we have covered in this hour.

Listing 6.16 Testing for a Function's Existence
   1: <html>   2: <head>   3: <title>Listing 6.16</title>   4: </head>   5: <body>   6: <?php   7:    8: function tagWrap( $tag, $txt, $func="" ) {   9:     if ( ! empty( $txt ) && function_exists( $func ) ) {  10:         $txt = $func( $txt );  11:         return "<$tag>$txt</$tag>\n";  12:     }  13:  }  14:   15: function underline( $txt ) {  16:     return "<u>$txt</u>";  17: }  18:   19: print tagWrap('b', 'make me bold');   20: // <b>make me bold</b>   21:   22: print tagWrap('i', 'underline me too', "underline");  23: // <i><u>underline me too</u></i>  24:   25: print tagWrap('i', 'make me italic and quote me',  26:     create_function('$txt', 'return "&quot;$txt&quot;";'));  27: // <i>&quot;make me italic and quote me&quot;</i>  28:   29: ?>  30: </body>  31: </html> 

We define two functions, tagWrap() (line 8) and underline() (line 15). The tagWrap() function accepts three strings: a tag, the text to be formatted, and an optional function name. It returns a formatted string. underline() requires a single argument the text to be formatted and returns the text wrapped in <u> tags.

When we first call tagWrap() on line 19, we pass it the character b and the string make me bold. Because we haven't passed a value for the function argument, the default value (an empty string) is used. On line 9, we check whether the $func variable contains characters and, if it is not empty, we call function_exists() to check for a function by that name. Of course, the $func variable is empty, so we wrap the $txt variable in <b> tags on line 11 and return the result.

We call tagWrap() on line 22 with the string 'i', some text, and a third argument: "underline". function_exists() finds a function called underline() (line 15), so it calls this function and passes the $txt argument variable to it before any further formatting is done. The result is an italicized, underlined string.

Finally, on line 25, we call tagWrap(), which wraps text in quotation entities. Of course, it would be quicker to simply add the entities to the text to be transformed ourselves, but this illustrates the point that function_exists() works as well on anonymous functions as it does on strings representing function names.

Put these lines into a text file called exists.php, and place this file in your Web server document root. When you access this script through your Web browser, it should look like Figure 6.8.

Figure 6.8. Output of Listing 6.16.


Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
Sams Teach Yourself PHP, MySQL and Apache in 24 Hours
ISBN: 067232489X
EAN: 2147483647
Year: 2005
Pages: 263

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net