As part of an integration against a third party service I need to calculate a check digit. I have been given a PHP snippet to show the algorithm but I need to implement it in pl/sql. The PHP snippet (taken from here) is:
function arvutaViitenumber($nr){
$nr = (string)$nr;
$kaal = array(7,3,1);
$sl = $st = strlen($nr);
$total = 0;
while($sl > 0 and substr($nr, --$sl, 1) >='0'){
$total += substr($nr, ($st-1)-$sl, 1)*$kaal[($sl%3)];
}
$kontrollnr = ((ceil(($total/10))*10)-$total);
return $nr.$kontrollnr;
}
I understand most of it except the substr($nr, --$sl, 1) >='0'
condition in the loop. There are two things I do not understand about this:
The main body of the loop takes each digit in order from the start. Why then have a condition where the loop exits before processing the nth digit from the start based on the state of the nth digit from the end?
When comparing a single-character string as greater or equal string
'0'
can the condition ever be false? In my tests it is always true, even if the string compared is a character not a number.
Question 1 is more of accademic intrest. My real interest is question 2 and how I will implement this check in the pl/sql version (or if I even need to?)