2013-05-17 38 views
3

標題解釋,但這裏就是我試圖做的:PHP_VERSION_ID是int,但未定義。 (PHP-FPM 5.4.4)

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頁在defined你可以這樣做:

<?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。

+0

如果您爲這種情況啓用它們,您應該會看到警告。 – hakre

回答

7

That's這裏所發生:

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

那是真實的, PHP_VERSION_ID的價值,你的情況,50404.

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

現在你實際上問定義(50404),並返回˚F ALSE。常數已經解決了它的價值。如果您想知道是否存在具有該名稱的常量,請將其設置爲引號:

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

* facepalm *我怎麼錯過了... – VuoriLiikaluoma

3

不能使用,如果它沒有定義的定義 - 讓你不得不測試它作爲一個字符串:

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); 
} 
相關問題