PHP Tip: Understanding the php -m Command for Viewing Loaded Modules

PHP Tip: Understanding the php -m Command for Viewing Loaded Modules
Photo by Winston Chen / Unsplash

When working with PHP, especially in environments where extensions and modules play a crucial role in functionality, knowing what is active and available in your PHP setup is essential. One handy command to get this information is php -m.

What is php -m?

The php -m command is a simple yet powerful tool in PHP that lists all the modules currently enabled in your PHP environment. It provides a quick overview of both built-in modules and any additional extensions that have been installed and activated.

Why Is It Important?

When you're debugging an issue, setting up a new PHP environment, or working with certain libraries, you may need to check whether a specific extension is installed. Many PHP applications rely on external modules such as pdo_mysql for database interactions or curl for making web requests. Using php -m helps you ensure that the necessary modules are available.

How to Use php -m

To use the php -m command, simply run it in your terminal or command line interface:

php -m

After running this command, you will see two categories of modules:

  1. Built-in Modules – These are modules that come bundled with PHP. Examples include date, spl, json, etc.
  2. External Extensions – These are modules that you have installed separately or enabled via your php.ini file. Common extensions include mysqli, mbstring, and gd.

The output will look something like this:

[PHP Modules]
bcmath
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
gd
hash
iconv
intl
json
libxml
mbstring
mysqli
mysqlnd
openssl
pcre
PDO
pdo_mysql
pdo_pgsql
Phar
random
readline
Reflection
session
SimpleXML
SPL
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

The list under [PHP Modules] shows the active PHP extensions, while [Zend Modules] displays any Zend extensions loaded (such as Xdebug).

Common Use Cases for php -m

  1. Checking Installed Extensions
    If a PHP script throws an error saying a specific module is missing (e.g., mbstring), you can quickly run php -m to verify if the extension is enabled.
  2. Verifying Extension Configuration
    After installing a new extension or modifying your php.ini file, running php -m ensures that the changes have been applied correctly.
  3. Troubleshooting Compatibility Issues
    Some PHP libraries require certain extensions to function. If an application isn't working as expected, php -m helps identify whether all required modules are present.

Bonus Tip: Combine with PHP Version Check

Another helpful command to use alongside php -m is php -v, which displays the current PHP version. This can be useful when troubleshooting compatibility issues between PHP versions and modules:

php -v

Finally

The php -m command is an essential tool for PHP developers and system administrators. It provides an easy way to see which PHP modules are currently enabled, helping you ensure your environment is properly configured for your applications.

Next time you're faced with a module-related issue or setting up a new environment, just remember: php -m is your go-to command for a quick module check!

Support Us

Subscribe to Buka Corner

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe