Swedish "personnummer"

Think of it like a social security number or something, everyone in sweden gets one. This is a easy calculation, the number itself looks something like
780101-8917
The different parts of the number have different meanings. The first six digits tells us that this person was born January 1, 1978. This is followed by a "-" where the next two digits is a code for the region this person was born in. The second last digit represents the gender on this person where and even number means it's a female, and odd number for a male.

The last number is a "control number" to validate the combination of digits. In the above example this is a "7", I'll now show how to calculate this, instructions quoted in-line.
 780101891 (all numbers except control one)
*212121212 (always use this digit string)
=1+4+8+1+0+1+1+6+9+2 > 33 (in result line split 10+ sums into single digits, then add)

10 - 3 = 7 (take 10 minus the last digit in above sum)

7 (this is th control digit for above example)
So why would this be a good thing to know? Well, if you every try to "make up an identity", registering at a swedish site or something like it - this place usually has an automated check of the control number. It's rather rare with a control against some "real" register, this simple check is usually it. To make it real simple, I'll add a calculator for you here.
The numbers 9 first digits (eg. 780101891)


Below is the code this form executes to calculate this. I "sorta" tried to keep it clean and fairly optimised, if I succeeded is up to you to tell I suppose. In the end it comes down to discrete mathematics when optimising algorithms ... and really, math wasnt ever my thing. Cheers.
<?
if(isset($_POST['strPnr'])){
    
$pnr $_POST['strPnr'];
    if(
strlen($pnr) == && is_numeric($pnr)){
            
// All ok, do the trick
            
$check = array(2,1,2,1,2,1,2,1,2);
            
$resarray = array(0);
            
            for(
$i=0;$i<9;$i++){
                if((
$pnr[$i] * $check[$i]) > 9){
                    
$resarray[] = substr($pnr[$i] * $check[$i], 01);
                    
$resarray[] = substr($pnr[$i] * $check[$i], 11);
                }else{
                    
$resarray[] = $pnr[$i] * $check[$i];
                }
            }
            
            if(
array_sum($resarray) > 9){
                
$final 10 substr(array_sum($resarray), 11);
            }else{
                
$final 10 array_sum($resarray);
            }
            
            echo 
"Control number is: " substr($final, (strlen($final)-1), 1);
    }else{
        echo 
"Incorrect numbers of digits provided or illegal characters used";
    }
}
?>