1.2. Finding Out Which Extensions Are Loaded

The first step in using PHP extensions is determining if the extension you need is built in or if you need to load it. PHP gives you several ways to get the information you need, depending on exactly what you want to know.

1.2.1. get_loaded_extensions()

The get_loaded_extensions() function gives you an array of every PHP function that's compiled into your PHP development environment. This function can be used on the command line:

$ php -r 'print_r(get_loaded_extensions());'

The results should look something like this:

$ Array
(
    [0] => xml
    [1] => tokenizer
    [2] => standard
    [3] => sockets
    [4] => session
    [5] => posix
    [6] => overload
    [7] => odbc
    [8] => mysql
    [9] => mbstring
    [10] => ldap
    [11] => ftp
    [12] => exif
    [13] => dbx
    [14] => curl
    [15] => ctype
    [16] => zlib
    [17] => pcre
)

The get_loaded_extensions() function can also be used within a script to deliver an array of loaded extensions.

1.2.2. extension_loaded()

Sometimes, you don't need to know everything; you just need a straight answer to the question "Is this extension available?" The extension_loaded() function gives you just that — a simple Boolean answer to a simple question. You can use it to dynamically load extensions from within your application code. Simply place the following lines of code at the top of your script:

if (!extension_loaded($extension)) {
   dl($extension);
}

1.2.3. php -m

If you're staring at a command line and wondering what extensions are available to you, the ...

Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.