Compare two version strings in PHP

后端 未结 5 880
遇见更好的自我
遇见更好的自我 2020-12-10 11:28

How to compare two strings in version format? such that:

version_compare(\"2.5.1\",  \"2.5.2\") => -1 (smaller)
version_compare(\"2.5.2\",  \"2.5.2\") =&         


        
5条回答
  •  萌比男神i
    2020-12-10 12:10

    From the PHP interactive prompt using the version_compare function, built in to PHP since 4.1:

    php > print_r(version_compare("2.5.1",  "2.5.2")); // expect -1
    -1
    php > print_r(version_compare("2.5.2",  "2.5.2")); // expect 0
    0
    php > print_r(version_compare("2.5.5",  "2.5.2")); // expect 1
    1
    php > print_r(version_compare("2.5.11", "2.5.2")); // expect 1
    1
    

    It seems PHP already works as you expect. If you are encountering different behavior, perhaps you should specify this.

提交回复
热议问题