PHP Iranian National ID Number Checker - PHP Function

Check the validity of an Iranian National ID Number using a PHP function. Ensure the code is 10 digits long and meets the required criteria.

<?php

function check_national_id($code = '') {
    if (!preg_match('/^[0-9]{10}$/', $code))
        return false;

    for ($i = 0; $i < 10; $i++)
        if (preg_match('/^' . $i . '{10}$/', $code))
            return false;

    for ($i = 0, $sum = 0; $i < 9; $i++)
        $sum += ((10 - $i) * intval(substr($code, $i, 1)));
        $ret = $sum % 11;
        $parity = intval(substr($code, 9, 1));

    if (($ret < 2 && $ret == $parity) || ($ret >= 2 && $ret == 11 - $parity))
        return true;

    return false;
}

?>