Current Location: Home> Function Categories> version_compare

version_compare

Comparison of two versions of "PHP normalized" numeric strings
Name:version_compare
Category:PHP Options and Information
Programming Language:php
One-line Description:Compare the two version numbers

Function name: version_compare()

Applicable version: PHP 4.1.0 and above

Usage: The version_compare() function is used to compare two version numbers.

Syntax: int version_compare ( string $version1 , string $version2 [, string $operator ] )

parameter:

  • version1: The first version number to be compared.
  • version2: The second version number to compare.
  • operator (optional): Compare operator. It can be one of the following values:
    • "<": less than
    • "<=": less than or equal to
    • ">": greater than
    • ">=": greater than or equal to
    • "==": equal to

Return value: If version1 is less than version2, return -1; if version1 is greater than version2, return 1; if both are equal, return 0.

Example:

 $version1 = "7.2.0"; $version2 = "7.3.0"; echo version_compare($version1, $version2); // 输出-1,因为7.2.0 小于7.3.0 echo version_compare($version1, $version2, "<"); // 输出true,因为7.2.0 小于7.3.0 echo version_compare($version1, $version2, ">="); // 输出false,因为7.2.0 不大于等于7.3.0 echo version_compare($version1, $version2, "=="); // 输出false,因为7.2.0 不等于7.3.0

The above example demonstrates the basic usage of the version_compare() function. You can pass different comparison operators according to your needs to compare version numbers and make corresponding logical judgments based on the return value.

Similar Functions
Popular Articles