Hi all,

I have to compare two large numbers to see if one is higher than the other.

An example of such a number is 89441000301200094490.

They are actually mobile phone SIM numbers.

I want to check to see if say 89441000301200094491 is higher than the number above (which it is).

I understand that I can't just use intval as these numbers are larger than is permitted.

So I built a funtion that works through each digit in each of the two numbers and checks if this is higher. It doesn't seem to be working though.

Can anyone see the problem with the code below or can anyone suggest another method for comparing these large numbers.

$theDbSimNum = "89441000301200122598";
	$theNewSimNum = "89441000301200397599";

for ( $counter = 0; $counter <= 19; $counter++) {
	$trimNew = intval(substr($theNewSimNum, $counter, 1));
	$trimHigh = intval(substr($theDbSimNum, $counter, 1));

	if($trimNew < $trimHigh){
		$theVerdict = "LOWER";
	} else if ($trimNew > $trimHigh){
		$theVerdict = "HIGHER";
	} else {
		$theVerdict = "UNKNOWN";
	}


}

echo $theVerdict;

    PHP can compare strings.

    $acct1 = "89441000301200094490";
    $acct2 = "89441000301200094491";
    echo "Acct1 is ".($acct1>$acct2 ? "higher" : "lower")." than Acct2.";
    

      Some other alternatives would be to use BCMath functions or [man]GMP[/man] functions.

        Yes, but in this case the fact is that these are IDs, and hence strings (or perhaps a serialized encoding of some more elaborate structure), rather than numbers. There's no arithmetic going on (what would be the significance of, say, 89441000301200094490 + 42?) so the fact that the strings consist entirely of Indo-Arabic numerals is spurious; the SIMs could identify themselves as "ivj3wi7ckj4sc" and "ivj3wi7cko8o0" without any loss of functionality.

          Hi weedPacket.

          I love the simplicity of your code but it doesn't seem to work at my end.

          If I adjust the values of $acct1 or $acct2, I get a result of lower even though the number is higher.

          The code I have used is below:

          <?php 
          $acct1 = "89441000301200094490"; 
          $acct2 = "89441000301200094489"; 
          echo "Acct1 is ".($acct1>$acct2 ? "higher" : "lower")." than Acct2."; 
          
          
          ?>

          Any further help would be much appreciated.

          Thanks

            Write a Reply...