donvo24600 2015-09-22 16:20
浏览 94
已采纳

php version_compare()在localhost上失败,在服务器上运行

This conditional code execution if statement is right out of the php manual:

// version_compare ( string $version1 , string $version2 [, string $operator ] )
// http://php.net/manual/en/function.version-compare.php
if (version_compare(PHP_VERSION, '5.3.0') >= 0)
{
    //echo 'I am at least PHP version 5.3.0, my version: ' . PHP_VERSION . "
";
    include_once( DOMAIN_PATH . "sitewide/functions/make_comparer.php" );
    usort($market_and_volume, make_comparer(['exchange_volume', SORT_DESC], ['exchange', SORT_DESC]));   
}

The above code runs free of error on the production server. On localhost php version 5.2.9-2 which theoretically fails the version_compare() function it throws an error:

Parse error: parse error, expecting `')'' in test.php on line 61

Normally i might not care since its running on the 5.4.39 production server, however I plan to use this snippet on almost every localhost page which as it stands would impede future development.

Any ideas?

  • 写回答

1条回答 默认 最新

  • dongzhunqiu4841 2015-09-22 16:25
    关注

    The problem is you are using the shorthand array notation. [...]. Unfortuantely this is only supported from PHP 5.4.0 and up. Even though the "bad" code is never run it fails to compile so throws an error.

    This can be fixed by using the array(...) notation like so.

    // version_compare ( string $version1 , string $version2 [, string $operator ] )
    // http://php.net/manual/en/function.version-compare.php
    if (version_compare(PHP_VERSION, '5.3.0') >= 0)
    {
        //echo 'I am at least PHP version 5.3.0, my version: ' . PHP_VERSION . "
    ";
        include_once( DOMAIN_PATH . "sitewide/functions/make_comparer.php" );
        usort($market_and_volume, make_comparer(array('exchange_volume', SORT_DESC), array('exchange', SORT_DESC)));   
    }
    

    REFERENCE: http://php.net/manual/en/language.types.array.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部