Question

How can I get a list of installed PHP extensions?

Answer and Explanation

To get a list of installed PHP extensions, you can use the PHP command-line interface (CLI) or a function within a PHP script.

Using the PHP CLI:

The most straightforward method is to use the command line. Open your terminal or command prompt and run the following command:

php -m

This command will output a list of all the compiled and enabled PHP extensions. Each extension will be listed on a new line.

For a more detailed output, which might include additional information such as the extension's version, use:

php -i | grep "extension_dir"

Using a PHP script:

Alternatively, you can use a PHP script to display the extensions list. Create a PHP file (e.g., extensions.php) with the following content:

<?php
$extensions = get_loaded_extensions();
echo "<ul>";
foreach ($extensions as $extension) {
  echo "<li>" . $extension . "</li>";
}
echo "</ul>";
?>

Save this file and access it via your web browser. It will display the loaded extensions as an unordered list.

Or if you want to get the extensions as an array:

<?php
$extensions = get_loaded_extensions();
print_r($extensions);
?>

Save this file and access it via your web browser. It will display the loaded extensions as an array.

Explanation:

- php -m: This is a quick way to see enabled extensions directly in the terminal. It lists the extensions that PHP has loaded.
- get_loaded_extensions(): This PHP function returns an array of all the loaded (compiled and enabled) extensions in your current PHP setup.

These methods will help you quickly identify the PHP extensions installed on your server, which can be crucial for debugging and ensuring your PHP applications function correctly. Remember that some extensions might be compiled into PHP but not enabled. Using these methods helps you see both compiled and enabled extensions.

More questions