PHP - gmp_cmp() 函式



定義和用法

gmp_cmp() 函式比較兩個 GMP 數字。

描述

gmp_cmp() 比較兩個給定的 GMP 數字,如果第一個數字大於第二個數字,則返回一個正數;如果相等,則返回 0;如果第一個數字小於第二個數字,則返回一個負數。

語法

gmp_cmp ( GMP $a , GMP $b ) : int

引數

序號 引數 & 描述
1

a

可以是 GMP 資源數字、gmp 物件或數字字串。

2

b

可以是 GMP 資源數字、gmp 物件或數字字串。

返回值

PHP gmp_cmp() 函式如果第一個數字大於第二個數字,則返回一個正數;如果相等,則返回 0;如果第一個數字小於第二個數字,則返回一個負數。

PHP 版本

此函式適用於 PHP 5.0.0 以上版本。

示例 1

gmp_cmp 的工作原理 -

<?php
   $case1 = gmp_cmp("3232", "1500"); // first number is greater
   $case2 = gmp_cmp("1500", "3232"); // first number is less than second number
   $case3 = gmp_cmp("1500", "1500"); // numbers are equal
   echo "Case 1 First number is greater : ".$case1;
   echo "<br/><br/>";
   echo "Case 2 Second number is greater : ".$case2;
   echo "<br/><br/>";
   echo "Case 3  Numbers are equal : ".$case3;
?>

這將產生以下結果 -

Case 1 First number is greater : 1
Case 2 Second number is greater : -1
Case 3 Numbers are equal : 0

示例 2

使用十六進位制數字的 gmp_cmp 的工作原理 -

<?php
   $case1 = gmp_cmp("0xFE", "0x80"); // first number is greater
   $case2 = gmp_cmp("0x80", "0xFE"); // first number is less than second number
   $case3 = gmp_cmp("0xFE", "0xFE"); // numbers are equal
   echo "Case 1 First number is greater : ".$case1;
   echo "<br/><br/>";
   echo "Case 2 Second number is greater : ".$case2;
   echo "<br/><br/>";
   echo "Case 3  Numbers are equal : ".$case3;
?>

這將產生以下結果 -

Case 1 First number is greater : 1
Case 2 Second number is greater : -1
Case 3 Numbers are equal
php_function_reference.htm
廣告