Extension functions

array get_loaded_extensions ( )

array get_extension_funcs ( string module_name)

int dl ( string module_name)

bool extension_loaded ( string name)

There are two functions that you will likely find useful for determining what extensions you have available to you, and these are get_loaded_extensions(), and get_extension_funcs(). In addition, the dl() function Dynamically Loads a PHP extension at runtime, which means you can enable a certain extension only for a certain script. Get_loaded_extensions() takes no parameters, and returns an array of the names of all extensions you have loaded. Get_extension_funcs() takes the name of an extension, and returns an array of the functions available inside that extension.

Using these two together it is easy to test whether you have an extension available to you, and, if so, that it contains the correct function. Remember that PHP versions contain varying amounts of functionality, so it is worth checking to make sure you have a function as opposed to just blindly calling it.

Take a look at the following code block:

    $extensions = get_loaded_extensions();

    foreach($extensions as $extension) {
        echo $extension;
        echo ' (', implode(', ', get_extension_funcs($extension)), ')<br />';

Breaking that down, it retrieves the names of all extensions currently loaded and cycles through them using a foreach loop. For each extension, it calls get_extension_funcs() to get the functions made available by that extension, then implodes that array into a string separated neatly by commas, then surrounds the whole thing in brackets. For example, if you have the wddx extension installed, you should see the following line somewhere in your output:

wddx (wddx_serialize_value, wddx_serialize_vars, wddx_packet_start, wddx_packet_end, wddx_add_vars, wddx_deserialize)

Note that the code uses echo rather than print because it uses the comma operator to chain together things to output, which is more efficient than using the concatenation operator. Put simply, the comma operator acts to pass several arguments to echo, which are output one by one individually. On the other hand, the concatenation operator, given three strings as in the example above, will combine strings one and two, then the new combined string with string three, then output the final combined string at once - having to chop and change strings twice is quite slow, and so should be avoided.

To load an extension at runtime you use the dl() function, passing in the name of the name of the extension to load its only parameter. This is only available when using PHP through the command line. Note that there are cross-platform considerations to using dl() that are discussed later. The downside to using dl() is that it needs to literally dynamically load and unload the extension each time your scripts run - this ends up being a great deal slower than running PHP as a web server module, where the extensions are loaded just once and kept in memory.

Here is an example of dl() in action on both Windows and Unix:

    dl('php_imap.dll'); // Windows
    dl('imap.so'); // Unix

If you just want to check whether a specific extension is loaded or not, without having to go through the fuss of sifting through the return value of get_loaded_extensions(), you can use the simple shortcut function extension_loaded(), which takes an extension name as its only parameter, and returns true if its loaded or false otherwise.


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: Pausing script execution >>

Previous chapter: Checking whether a function is available

Jump to:


Home: Table of Contents

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