How to make your PHP application check for its dependencies

The very informative phpinfo() function

The phpinfo() function displays just about everything you want to know about your PHP installation. It includes info on all your PECL and PEAR modules so it is a quick way to check what’s installed. It will also tell you useful web server information including any query strings you pass. To display all this good info just point your browser at a page on your server that contains the following code:

<?php
 
phpinfo();
 
?>

That’s it!

Automatic dependency checking

The phpinfo() function will give us a page that displays a lot of info. You can pass it bitwise constants to narrow down the information displayed but what if we want to check for specific items?

In my humble opinion, when developing software or anything in general, it is a good idea to design things so that the end user will not even need a manual because the user interface is obvious. When something doesn’t work, it should be equally obvious how to fix it.

If you write a PHP application that others will install and use, it is a good idea to check for dependencies when they try to use the application. This way even if they don’t read your documentation they will quickly know why the software is not working.

Using phpversion(), PHP_VERSION, and version_compare() to check the PHP verson

To get the core PHP version you can use either of the following methods:

<?php
 
echo phpversion();
echo "<br/>or<br/>";
echo PHP_VERSION;
 
?>

The above code should output something like this:

5.2.6-2ubuntu4
or
5.2.6-2ubuntu4

If you are using Ubunto or some other distribution, you will note that some additional stuff is tack on to the version number (I.e. “-2ubuntu4”). This makes a comparison to your expected version a little tricky but you can use a substr()/strpos() combo to get what you need. There is an easier way to do the comparison though. The version_compare() function is “PHP-standardized” version aware. So we can do something like this:

<?php
 
if (version_compare(PHP_VERSION, '5.0.0', '<')) {
    echo 'You are using PHP version ' . PHP_VERSION . 
      'This program requires PHP version 5.0.0 or higher.';
} else {
    echo 'You are using PHP 5.0.0 or higher. You are all set!';
}
 
?>

Now you can check the PHP version and notify the user if it is not the minimum required version.

The PHP function documentation for each function at www.php.net include the PHP versions that contain the function in the upper left hand corner:

substr function version on php.net

You can use this to learn what versions of PHP include the functions you are using in your code to help identify your minimum PHP version requirement.

Using get_loaded_extensions() to check for extensions

The get_loaded_extensions() function will return an array of PHP extensions that you can use to check if a specific extension is installed. Use it in combination with with the in_array() function to check if the extension you require is loaded. In this example I check if the PECL_HTTP module is installed:

<?php
 
if (in_array("http",get_loaded_extensions())){
    echo 'The PECL_HTTP module is installed. '.
      'You are all set!';
} else {
    echo 'The PECL_HTTP module is not installed. '.
      'Please install it.';
}
 
?>

You can use the phpversion() function to check if extension is listed and if so, its version. This code example not only checks if the PECL_HTTP module is installed, but also checks it’s version:

<?php
 
if (!phpversion('http')){
    echo 'The PECL_HTTP module is not installed. '.
      'Please download it from '.
      '<a href="http://pecl.php.net/package/pecl_http">here</a> '.
      ' and install it.';
} else {
    if (version_compare(phpversion('http'),'1.6.0','>=')){
        echo 'The PECL_HTTP extension is installed and '.
          'version 1.6.0 or higher. You are all set!';
    } else {
        echo 'Please upgrade your PECL_HTTP extension to '.
          'version 1.6.0 or higher. You can download it '.
          '<a href="http://pecl.php.net/package/pecl_http">here'.
          '</a>.';
    }
}
 
?>

Use function_exists() to check for individual functions

So far the methods for checking dependencies have been somewhat broad. They check that the script has a certain version of PHP or extensions installed and that will likely be good enough in most cases. If you really want to be thorough you can also check if specific functions are available using the function_exists() method. In this example I check that the http_request() module, which is part of the PECL_HTTP extension, is there before I use it. If it is not, I use the less featured, built in, file_get_contents() function.

<?php
 
if (function_exists("http_get")) {
    echo 'Using the http_get():<br/>' .
        http_parse_message(http_get("http://www.example.com"))->body;
} else {
    echo 'Using the file_get_contents():<br/>' . 
        file_get_contents("http://www.example.com");
}
 
?>

Check for include files

Here is a simple way to check for include files. It doesn’t verify their content but you can at least make sure they are there:

<?php
 
if (!file_exists('httptest.php')){
    die('The httptest.php include file is missing.');
} else {
    require_once('httptest.php');
}
 
?>

Wrap up

Checking dependencies is an important part of building robust software and hopefully the above techniques will help accomplish that. Even if your end user is a very technical they will likely appreciate a good dependency checking mechanism that quickly tells them whats missing to save them time. If your software will be used by non-technical users you might want to automatically and gracefully downgrade your software feature set instead of generating errors and asking them for something they won’t know how to do. Usability is king!

Leave a Reply