New Global Constants

PHP 7 introduces a number of new constants.

var_dump(PHP_FLOAT_DIG);
var_dump(PHP_FLOAT_EPSILON);
var_dump(PHP_FLOAT_MIN);
var_dump(PHP_FLOAT_MAX);

On a 64bit Linux system, these constants evaluate to:

int(15)
double(2.2204460492503E-16)
double(2.2250738585072E-308)
double(1.7976931348623E+308)

PHP_FLOAT_DIG holds the number of digits that can be rounded into a float and back without precision loss.

PHP_FLOAT_EPSILON holds the smallest representable positive number that, when added to 1.0, will result in a value different from 1.0. This value is useful when comparing floating point numbers: due to the nature of floating point arithmetic, float results are never exact, but there are always rounding errors:

$a = 0.1 + 0.2;
$b = 0.3;

var_dump($a);
var_dump($b);
var_dump($a == $b);

You might expect the comparison to return true, but the result is:

double(0.3)
double(0.3)
bool(false)

When comparing two floating point numbers, you have to allow for a difference of less than PHP_FLOAT_EPSILON:

$a = 0.1 + 0.2;
$b = 0.3;

var_dump($a);
var_dump($b);
var_dump($a - $b < PHP_FLOAT_EPSILON);

Now the comparison works as expected:

double(0.3)
double(0.3)
bool(true)

The two constants PHP_FLOAT_MIN and PHP_FLOAT_MAX represent the smallest and biggest floating point number that PHP can represent, analogous to PHP_INT_MIN and PHP_INT_MAX.

The PHP_OS_FAMILY Constant

Not all operating systems are equal. PHP usually does a great job abstracting away many of those subtleties. In fact, PHP 7 was a giant leap forward in closing quite a few gaps between Unix/Linux and Windows. However, sometimes an application just needs to know which operating system it runs on, for example to be able to select and execute system-specific code.

Before PHP 7.2, it was rather inconvenient to detect the family of operating systems a PHP application was being executed on. You had to use code like this:

function detect_os_family(): string
{
    if (DIRECTORY_SEPARATOR === '\\') {
        return 'Windows';
    }

    switch (PHP_OS) {
        case 'Darwin':
            return 'Darwin';

        case 'DragonFly':
        case 'FreeBSD':
        case 'NetBSD':
        case 'OpenBSD':
            return 'BSD';

        case 'Linux':
            return 'Linux';

        case 'SunOS':
            return 'Solaris';

        default:
            return 'Unknown';
    }
}

PHP 7.2 introduces a new constant, PHP_OS_FAMILY, that makes the code shown above superfluous. This constant is pre-defined and is set to one of the following possible strings: BSD, Darwin, Linux, SunOS, Windows, or Unknown.

Please note that, for various reasons, it can get extremely difficult to determine the exact version of the running operating system. You should never write PHP code that depends on specific operating system versions.

The PHP_INT_MIN Constant

The PHP_INT_MIN constant complements the PHP_INT_MAX constant. It holds the smallest integer supported in the build of PHP that is used:

php -r 'var_dump(PHP_INT_MIN);'
int(-9223372036854775808)