[ Team LiB ] Previous Section Next Section

Testing for Function Existence

You have seen that you do not always know that a function exists before you try to invoke it. If your code were to work with a function name stored in a variable, for example, it would be useful for you to be able to test whether the function exists before you attempted to call it. Furthermore, different builds of the PHP engine can include different functionality. If you are writing a script that might be run on multiple servers, you might want to check that key features are available. You might write code that will use MySQL if mysql 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 returns 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 covered in this chapter.

Listing 6.16 Testing for a Function's Existence
 1: <!DOCTYPE html PUBLIC
 2:   "-//W3C//DTD XHTML 1.0 Strict//EN"
 3:   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
 4: <html>
 5: <head>
 6: <title>Listing 6.16</title>
 7: </head>
 8: <body>
 9: <div>
10: <?php
11:
12: function tagWrap( $tag, $txt, $func="" ) {
13:   if ( function_exists( $func ) )
14:     $txt = $func( $txt );
15:   return "<$tag>$txt</$tag>\n";
16: }
17:
18: function subscript( $txt ) {
19:   return "<sub>$txt</sub>";
20: }
21:
22: print tagWrap('b', 'make me bold');
23: // <b>make me bold</b>
24:
25: print tagWrap('i', 'shrink me too', "subscript");
26: // <i><sub>shrink me too</sub></i>
27:
28: print tagWrap('i', 'make me italic and quote me',
29:   create_function('$txt', 'return ""$txt"";'));
30: // <i>"make me italic and quote me"</i>
31:
32: ?>
33: </div>
34: </body>
35: </html>

We define two functions, tagWrap() (line 12) and subscript() (line 18). TagWrap() accepts three strings, a tag, the text to be formatted, and an optional function name. It returns a formatted string. subscript() requires a single argument, the text to be formatted, and returns the text wrapped in <sub> tags.

When we first call tagWrap() on line 22, 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 13, 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 15 and return the result.

We next call tagWrap() on line 25 with the string i, some text, and a third argument: subscript. function_exists() does find a function called subscript() (line 13), so this is called and passed the $txt argument variable before any further formatting is done. The result is an italicized string rendered as subscript.

Finally, we call tagWrap() on line 28 with an anonymous function (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 does illustrate the point that function_exists() works as well on anonymous functions as it does on strings representing function names.

    [ Team LiB ] Previous Section Next Section