- str_repeat: documentation ( source)
- range: documentation ( source)
<?php
$prefix = (string) 1234;
$id = (string) 3;
$number = $prefix . str_repeat('0', 12 - strlen($prefix) - strlen($id)) . $id;
$odd = true;
$checksum = 0;
$key = range(0, 9);
for ($i = strlen($number); $i > 0; $i--) {
$checksum += $key[$number[$i - 1]] * (($odd = !$odd) ? 3 : 1);
}
$checksum = 10 - $checksum % 10;
$checksum = ($checksum == 10) ? 0 : $checksum;
echo $number . $checksum;