PHP_VERSION_ID是int但未定义. (PHP-FPM 5.4.4)

参见英文答案 > How to get useful error messages in PHP?                                    29个
标题解释了它,但这是我试图做的:

if (!defined(PHP_VERSION_ID) || PHP_VERSION_ID < 50400) {
    trigger_error('PHP version 5.4 or above is required to run this code. Please upgrade to continue...', E_USER_ERROR);
}

出于某种原因,这是正在发生的事情:

var_dump(PHP_VERSION_ID);          // returns int(50404)
var_dump(defined(PHP_VERSION_ID)); // returns bool(false)

根据PHP.net页面定义你可以这样做:

<?PHP
// PHP_VERSION_ID is available as of PHP 5.2.7, if our 
// version is lower than that, then emulate it
if (!defined('PHP_VERSION_ID')) {
    $version = explode('.', PHP_VERSION);

    define('PHP_VERSION_ID', ($version[0] * 10000 + $version[1] * 100 + $version[2]));
}

// PHP_VERSION_ID is defined as a number, where the higher the number 
// is, the newer a PHP version is used. It's defined as used in the above 
// expression:
//
// $version_id = $major_version * 10000 + $minor_version * 100 + $release_version;
//
// Now with PHP_VERSION_ID we can check for features this PHP version 
// may have, this doesn't require to use version_compare() everytime 
// you check if the current PHP version may not support a feature.
//
// For example, we may here define the PHP_VERSION_* constants thats 
// not available in versions prior to 5.2.7

if (PHP_VERSION_ID < 50207) {
    define('PHP_MAJOR_VERSION',   $version[0]);
    define('PHP_MInor_VERSION',   $version[1]);
    define('PHP_RELEASE_VERSION', $version[2]);

    // and so on, ...
}
?>

关于为什么这不起作用的任何想法?我在Debian Wheezy上运行PHP-FPM 5.4.4.

解决方法:

这就是这里发生的事情

var_dump(PHP_VERSION_ID);          // returns int(50404)

这是真的,PHP_VERSION_ID的值在您的情况下是50404.

var_dump(defined(PHP_VERSION_ID)); // returns bool(false)

现在你实际上是在询问已定义的(50404),并返回false.常数得到了解决它的价值.如果您想知道是否存在具有该名称的常量,请将其设置为引号:

    var_dump(defined('PHP_VERSION_ID')); // returns bool(true)

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...