3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Test { public static function validateUPCA($value) { $upc = strval($value); $stringLength = strlen($upc); if($stringLength < 12 || $stringLength > 13 || intval($upc)==0) { return false; } $odd_sum = 0; $even_sum = 0; for($i = 0; $i < $stringLength - 1; $i++) { if($i % 2 === 1) { $even_sum += $upc[$i]; } else { $odd_sum += $upc[$i]; } } if ($stringLength == 12) { // UPC-A format. Odd Sum * 3 $total_sum = $even_sum + $odd_sum * 3; } else { // EAN format. Even Sum * 3 $total_sum = $odd_sum + $even_sum * 3; } $modulo10 = $total_sum % 10; $check_digit = $modulo10 == 0 ? 0 : 10 - $modulo10; return $upc[$stringLength - 1] == $check_digit; } } $res = Test.validateUPCA(840018115309); echo "res: $res\n";
based on irZ9r
Output for 7.2.0
Warning: Use of undefined constant Test - assumed 'Test' (this will throw an Error in a future version of PHP) in /in/PlPTE on line 46 Fatal error: Uncaught Error: Call to undefined function validateUPCA() in /in/PlPTE:46 Stack trace: #0 {main} thrown in /in/PlPTE on line 46
Process exited with code 255.
Output for 7.0.0 - 7.1.10
Notice: Use of undefined constant Test - assumed 'Test' in /in/PlPTE on line 46 Fatal error: Uncaught Error: Call to undefined function validateUPCA() in /in/PlPTE:46 Stack trace: #0 {main} thrown in /in/PlPTE on line 46
Process exited with code 255.
Output for 5.3.19 - 5.6.28
Notice: Use of undefined constant Test - assumed 'Test' in /in/PlPTE on line 46 Fatal error: Call to undefined function validateUPCA() in /in/PlPTE on line 46
Process exited with code 255.