Reputation: 43
Basically I'm working on a card management system in a particular module that has the job of creating bulk cards that are sequential. These cards are 19 digits long and because these cards have a monetary value I need to store the entire card value. The odd thing is that the system has no trouble managing VISA card number incrementing and those are 16 digits long. I'm assuming those last 3 digits are what is breaking the function but I have no idea how on Earth to handle this as I've never had to deal with such large values before.
$seqArray = array();
for($i = $_POST['startcardnumber']; $i <= $_POST['endcardnumber']; $i++) {
$i = sprintf('%0.0f',$i);
if(strlen($i) < $count) { $i = str_pad($i, $count, '0', STR_PAD_LEFT); }
array_push($seqArray, $i);
}
Any help is much appreciated.
Thanks to Fluffeh I found out that the BC Math functions were exactly what I needed. Below is the new for loop that I'm using to calculate and increment card numbers.
$seqArray = array();
for($s = $_POST['startcardnumber'], $e = $_POST['endcardnumber'];bccomp($s,$e) != 1; $s = bcadd($s, 1)) {
if(strlen($s) < $count) { $s = str_pad($s, $count, '0', STR_PAD_LEFT); }
array_push($seqArray, $s);
}
Upvotes: 3
Views: 221
Reputation: 33522
The BC Math library might be a little tedious to work with, but it will handle numbers in values you need with ease.
The downside is that you can't use simple things operators as expected:
For example, addition is done as the following - using the bcadd() function:
<?php
$a = '1.234';
$b = '5';
echo bcadd($a, $b); // 6
echo bcadd($a, $b, 4); // 6.2340
?>
Upvotes: 2