PHP extensions add runtime modules such as pdo_mysql, gd and intl.
What It Does
PHP extensions add runtime modules such as pdo_mysql, gd and intl. The following examples distinguish a normal use case, a boundary case and a practical application.
Requirements and Compatibility
PHP 8.x and a local test environment. Apply input validation, correct error handling and the requirements of any optional libraries.
The code focuses on the core concept. Production implementation may also need routes, validation, persistence, authorization or deployment tooling.
Example 1: Basic Behavior
List installed CLI modules and the loaded configuration files.
php -m
php --ini
This is a command-line example; use it in its appropriate file or shell, not in the PHP source editor.
Example 2: Another Case
Confirm the specific PDO or other module needed by the application.
php -r "var_dump(extension_loaded('pdo_mysql'));"
This is a command-line example; use it in its appropriate file or shell, not in the PHP source editor.
Practical Example: Putting It to Use
Handle missing extensions before entering the application code path.
<?php
$required = ["json", "openssl", "pdo"];
$missing = array_filter($required, fn ($ext) => !extension_loaded($ext));
echo $missing ? implode(", ", $missing) : "All required modules loaded";
Important Behavior and Edge Cases
An extension available in CLI may be missing in PHP-FPM, and php.ini loading differs by SAPI.
Comparison with Related Functions or Approaches
| Option | What it means in practice |
|---|---|
| Managing Extensions | PHP extensions add runtime modules such as pdo_mysql, gd and intl. |
| Related approach | Check extension_loaded() and the deployed runtime's module list. |
Common Beginner Mistakes
| Mistake | Correct approach |
|---|---|
| Ignoring the important boundary for Managing Extensions | An extension available in CLI may be missing in PHP-FPM, and php.ini loading differs by SAPI. |
| Assuming a related mechanism has the same behavior | Check extension_loaded() and the deployed runtime's module list. |
Key Takeaways
- PHP extensions add runtime modules such as pdo_mysql, gd and intl.
- An extension available in CLI may be missing in PHP-FPM, and php.ini loading differs by SAPI.
- Check extension_loaded() and the deployed runtime's module list.
Before adapting these examples, verify the return values, test the failure paths and confirm that your application meets its security and environment requirements.