dl()

dl() — Loads a PHP extension at runtime.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

What it does

Loads the PHP extension given by the parameter extension_filename.

Signature

dl(string $extension_filename): bool

Parameters

ParameterTypeWhat it takes
$extension_filenamestringThis parameter is only the filename of the extension to load which also depends on your platform. For example, the sockets extension (if compiled as a shared module, not the default!) would be called sockets.so on Unix platforms whereas it is called php_sockets.dll on the Windows platform.

Return value

Returns true on success or false on failure. If the functionality of loading modules is not available or has been disabled (by setting enable_dl off in php.ini) an E_WARNING is emitted and false is returned. If dl() fails because the specified library couldn't be loaded, in addition to false an E_WARNING message is emitted.

Example

A runnable snippet:

// Example loading an extension based on OS if (!extension_loaded('sqlite')) { if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { dl('php_sqlite.dll'); } else { dl('sqlite.so'); } } // Or using PHP_SHLIB_SUFFIX constant if (!extension_loaded('sqlite')) { $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : ''; dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX); }

See also

extension_loaded()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page dl() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.