How to Handle Unloaded PHP Extensions at Runtime – SitePoint » PHP
Mar 25, 2010
For the purposes of this article, we?ll assume you need to create or manipulate images and require PHP?s GD library. To load the GD library on your system: But what happens when you want to move your web application to another host or platform where a different set of extensions are configured? PHP provides an extension_loaded(name) function which returns true when the named library is available, e.g. Alternatively, you can check for the existence of specific library function using function_exists(), e.g. However, function_exists() is more risky ? another developer could write their own function named ?gd_info?. I?d recommend using function_exists() in situations when a function has been introduced in a later version of PHP. For example, if your application runs in both PHP4 and PHP5, you could check for the existance of imagefilter() (a PHP5-only GD function) before attempting to modify an image. PHP provides a dl() function for dynamically loading extensions at runtime. Unfortunately: I?d recommend avoiding dl(). That leaves us with three options: Do you have any other tips for handling missing PHP extensions? <script src=”http://adscluster.aws.sitepoint.com/openx/adjs.sp.php?region=14&did=adz&adtype=vertical” type=”text/javascript”> Related posts:
Unless you?re creating very simple applications, you will soon require one or more PHP extensions. Extensions are code libraries which extend the core functionality of the language.Loading an Extension
--with-gd.Checking an Extension is Loaded
<?php
if (extension_loaded('gd')) {
echo 'GD extension is loaded and everything is fine!';
}
else {
echo 'Where is the GD library?';
exit();
}
?>
<?php
if (function_exists('gd_info')) {
echo 'gd_info() is available so the GD library is probably available.';
}
else {
echo 'gd_info() cannot be found?';
exit();
}
?>
Handling Unloaded Extensions

