{"id":21283,"date":"2022-11-12T21:58:44","date_gmt":"2022-11-12T16:28:44","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/"},"modified":"2022-11-12T21:58:44","modified_gmt":"2022-11-12T16:28:44","slug":"solved-php-calculation-modulus-10-check-digit-generation-closed","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/","title":{"rendered":"[Solved] PHP Calculation &#8211; Modulus 10 check digit generation [closed]"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-35953826\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"35953826\" data-parentid=\"35953507\" data-score=\"2\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>In case someone gets to the question wondering how to use modulus or &#8216;getting the rest of a division&#8217; (<code>%<\/code>):<\/p>\n<pre><code>echo 5 % 3; \/\/ outputs 2\n<\/code><\/pre>\n<\/p>\n<hr>\n<p><em>(You can take a look at a step-by-step output after this first part)<\/em><\/p>\n<p>This is the commented function:<\/p>\n<pre><code>function func_name($docnum){\n\n    \/\/ make sure there is just numbers in $docnum\n    $docnum = preg_replace(\"\/[^0-9]\/\",\"\",$docnum);      \n\n    \/\/ change order of values to use in foreach\n    $vals = array_reverse(str_split($docnum));\n\n    \/\/ multiply every other value by 2\n    $mult = true;\n    foreach($vals as $k =&gt; $v){\n        $vals[$k] = $mult ? $v*2: $v;\n        $vals[$k] = (string)($vals[$k]);\n        $mult = !$mult;\n    }\n\n    \/\/ checks for two digits (&gt;9)\n    $mp = array_map(function($v){\n        return ($v &gt; 9) ? $v[0] + $v[1] : $v;\n    }, $vals);\n\n    \/\/ adds the values\n    $sum = array_sum($mp);\n\n    \/\/gets the mod\n    $md = $sum % 10;\n\n    \/\/ checks how much for 10\n    $result = 10 - $md;\n\n    \/\/ returns the value\n    return $result;\n}\n<\/code><\/pre>\n<hr>\n<p>Some test runs:<\/p>\n<pre><code>$docnum = 5843;\necho func_name($docnum);\n<\/code><\/pre>\n<p>Output:<\/p>\n<blockquote>\n<p>8<\/p>\n<\/blockquote>\n<hr>\n<pre><code>$docnum = 1111;\necho func_name($docnum);\n<\/code><\/pre>\n<p>Output:<\/p>\n<blockquote>\n<p>4<\/p>\n<\/blockquote>\n<hr>\n<pre><code>$docnum = '-5a84fadsafds.fdar3';\necho func_name($docnum);\n<\/code><\/pre>\n<p>Output:<\/p>\n<blockquote>\n<p>8<\/p>\n<\/blockquote>\n<hr>\n<pre><code>$docnum = 4321;\necho func_name($docnum);\n<\/code><\/pre>\n<p>Output:<\/p>\n<blockquote>\n<p>6<\/p>\n<\/blockquote>\n<hr>\n<p>This codes outputs the steps&#8230;<\/p>\n<pre><code>$docnum = '-5a84fadsafds.fdar3';\n\necho 'Original Value: ';\nprint_r($docnum);\necho '&lt;hr&gt;';\n\n$docnum = preg_replace(\"\/[^0-9]\/\",\"\",$docnum); \n\necho 'Numbers only: ';\nprint_r($docnum);\necho '&lt;hr&gt;';\n\n$vals = array_reverse(str_split($docnum));\n\necho 'Reversed Value: ';\nprint_r($vals);\necho '&lt;hr&gt;';\n\n$mult = true;\nforeach($vals as $k =&gt; $v){\n    $vals[$k] = $mult ? $v*2: $v;\n    $vals[$k] = (string)($vals[$k]);\n    $mult = !$mult;\n}\n\necho 'After mult.: ';\nprint_r($vals);\necho '&lt;hr&gt;';\n\n$mp = array_map(function($v){\n    return ($v &gt; 9) ? $v[0] + $v[1] : $v;\n}, $vals);\n\necho 'Checked for &gt;9: ';\nprint_r($mp);\necho '&lt;hr&gt;';\n\n$sum = array_sum($mp);\n\necho 'All values together: ';\nprint_r($sum);\necho '&lt;hr&gt;';\n\n$md = $sum % 10;\n\necho 'Mod: ';\nprint_r($md);\necho '&lt;hr&gt;';\n\n$result = 10 - $md;\n\necho 'Final result: ';\nprint_r($result);\necho '&lt;hr&gt;';\n<\/code><\/pre>\n<p>Output:<\/p>\n<blockquote>\n<p>Original Value: -5a84fadsafds.fdar3 <br \/>\n  Numbers only: 5843 <br \/>\n  Reversed Value: Array ( [0] =&gt; 3 [1] =&gt; 4 [2] =&gt; 8 [3] =&gt; 5 )  <br \/>\n  After mult.: Array ( [0] =&gt; 6 [1] =&gt; 4 [2] =&gt; 16 [3] =&gt; 5 )  <br \/>\n  Checked for &gt;9: Array ( [0] =&gt; 6 [1] =&gt; 4 [2] =&gt; 7 [3] =&gt; 5 )  <br \/>\n  All values together: 22 <br \/>\n  Mod: 2 <br \/>\n  Final result: 8 <\/p>\n<\/blockquote>\n<hr>\n<p>Read more about things used in the code:<\/p>\n<ul>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/function.array-reverse.php\">http:\/\/php.net\/manual\/en\/function.array-reverse.php<\/a><\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/function.array-map.php\">http:\/\/php.net\/manual\/en\/function.array-map.php<\/a><\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/function.array-sum.php\">http:\/\/php.net\/manual\/en\/function.array-sum.php<\/a><\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/control-structures.foreach.php\">http:\/\/php.net\/manual\/en\/control-structures.foreach.php<\/a><\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/language.operators.arithmetic.php\">http:\/\/php.net\/manual\/en\/language.operators.arithmetic.php<\/a> (modulus)<\/li>\n<li><a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/php.net\/manual\/en\/language.types.type-juggling.php\">http:\/\/php.net\/manual\/en\/language.types.type-juggling.php<\/a> (<code>(string)<\/code>)<\/li>\n<\/ul>\n<hr>\n<p>As suggested by @Floris, you can replace the double-digit sum part with modulus 9, so instead of<\/p>\n<pre><code>$mp = array_map(function($v){\n        return ($v &gt; 9) ? $v[0] + $v[1] : $v;\n    }, $vals);\n<\/code><\/pre>\n<p>you would have<\/p>\n<pre><code>$mp = array_map(function($v){\n        return $v % 9;\n    }, $vals);\n<\/code><\/pre>\n<p>and still keep the same output&#8230;<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">6<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved PHP Calculation &#8211; Modulus 10 check digit generation [closed] <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] In case someone gets to the question wondering how to use modulus or &#8216;getting the rest of a division&#8217; (%): echo 5 % 3; \/\/ outputs 2 (You can take a look at a step-by-step output after this first part) This is the commented function: function func_name($docnum){ \/\/ make sure there is just numbers &#8230; <a title=\"[Solved] PHP Calculation &#8211; Modulus 10 check digit generation [closed]\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/\" aria-label=\"More on [Solved] PHP Calculation &#8211; Modulus 10 check digit generation [closed]\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[4884,339],"class_list":["post-21283","post","type-post","status-publish","format-standard","hentry","category-solved","tag-modulus","tag-php"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] PHP Calculation - Modulus 10 check digit generation [closed] - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] PHP Calculation - Modulus 10 check digit generation [closed] - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] In case someone gets to the question wondering how to use modulus or &#8216;getting the rest of a division&#8217; (%): echo 5 % 3; \/\/ outputs 2 (You can take a look at a step-by-step output after this first part) This is the commented function: function func_name($docnum){ \/\/ make sure there is just numbers ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-12T16:28:44+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] PHP Calculation &#8211; Modulus 10 check digit generation [closed]\",\"datePublished\":\"2022-11-12T16:28:44+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/\"},\"wordCount\":190,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"modulus\",\"php\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/\",\"name\":\"[Solved] PHP Calculation - Modulus 10 check digit generation [closed] - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-11-12T16:28:44+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] PHP Calculation &#8211; Modulus 10 check digit generation [closed]\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] PHP Calculation - Modulus 10 check digit generation [closed] - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] PHP Calculation - Modulus 10 check digit generation [closed] - JassWeb","og_description":"[ad_1] In case someone gets to the question wondering how to use modulus or &#8216;getting the rest of a division&#8217; (%): echo 5 % 3; \/\/ outputs 2 (You can take a look at a step-by-step output after this first part) This is the commented function: function func_name($docnum){ \/\/ make sure there is just numbers ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/","og_site_name":"JassWeb","article_published_time":"2022-11-12T16:28:44+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] PHP Calculation &#8211; Modulus 10 check digit generation [closed]","datePublished":"2022-11-12T16:28:44+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/"},"wordCount":190,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["modulus","php"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/","url":"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/","name":"[Solved] PHP Calculation - Modulus 10 check digit generation [closed] - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-12T16:28:44+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-php-calculation-modulus-10-check-digit-generation-closed\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] PHP Calculation &#8211; Modulus 10 check digit generation [closed]"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/21283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=21283"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/21283\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=21283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=21283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=21283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}