Sometimes you want to make sure a certain PHP extension is loaded or not. These extensions can be curl, xmlrpc, or ldap. After all, when you move your application from one server to another the configuration is not necessarily the same. So here’s how to check for loaded extensions using get_loaded_extensions().
1 2 3 4 5 6 7 8 9 10 | $extNeeded = array('ldap', 'mysql', 'xmlrpc', 'ron_ext'); $loadedExtension = get_loaded_extensions(); foreach ($extNeeded as $ext) { if (!in_array($ext, $loadedExtension)) { echo 'The application cannot start properly, '.$ext.' extension is missing. The following extensions are needed: '.implode(', ', $extNeeded).'. Terminating application ...'; die; } } |
A few useful additions.
For checking just one extension, extension_loaded() is more direct than scanning the array returned by get_loaded_extensions():
1 2 3 |
It’s a one-liner, doesn’t allocate the full extension list, and reads a little nicer at call sites. Use get_loaded_extensions() when you genuinely need to iterate over many at once (like the original example does), and extension_loaded() for single checks.
You can also collect all the missing ones into a single message instead of bailing on the first one — better feedback for the operator:
1 2 3 4 5 6 | $required = ['curl', 'ldap', 'mbstring', 'pdo_mysql']; $missing = array_filter($required, fn($ext) => !extension_loaded($ext)); if (!empty($missing)) { die('Missing required PHP extensions: ' . implode(', ', $missing)); } |
If you’d rather not write any PHP at all, the command line gives you the same answer:
1 2 3 | php -m | grep -i curl php -m # full list of compiled-in + loaded modules php --ri curl # detailed runtime info for one extension (version, settings) |
php -m shows what your CLI php binary has loaded; if you’re chasing a problem that only shows up under the web server, you’ll want to run phpinfo() from a web request instead — the CLI and FPM SAPIs load different php.ini files and can have different extensions.
One note on the original example: two of the four extensions listed (mysql and xmlrpc) are no longer part of modern PHP. The mysql extension was removed in PHP 7.0 — use mysqli or pdo_mysql instead. xmlrpc was unbundled in PHP 8.0 and is effectively unmaintained; if you still need XML-RPC, the php-xmlrpc PECL package is technically available, but most projects move to JSON-RPC or REST these days. The check pattern in the post is still correct — it’s just that the specific extension names you’d put in $extNeeded have evolved.