Call to undefined function apache_get_version

The PHP function apache_get_version returns a string that contains the version number of the Apache web server that PHP is currently running on.

Well… that is what it is supposed to do.

The other day, I saw one developer on Stack Overflow struggling with the following fatal error.

Fatal error: Uncaught Error: Call to undefined function apache_get_version()

Basically, the function did not exist. Why did it not exist?

Well, this usually means that PHP has not been compiled to run as a normal Apache module. That, or the developer is attempting to use the function in a CLI command line script (in this case, PHP is not running on Apache).

A basic workaround to this problem could look something like this.

<?php

if(!function_exists('apache_get_version')){
    function apache_get_version(){
        if(!isset($_SERVER['SERVER_SOFTWARE']) || strlen($_SERVER['SERVER_SOFTWARE']) == 0){
            return false;
        }
        return $_SERVER["SERVER_SOFTWARE"];
    }
}

echo apache_get_version();

In the simple example above, we check to see if the function exists. If it doesn’t, then we create a custom function that returns whatever string is stored in the SERVER_SOFTWARE superglobals variable.