3v4l.org

run code in 300+ PHP versions simultaneously
<?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";
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Fatal error: Uncaught Error: Undefined constant "Test" in /in/PlPTE:46 Stack trace: #0 {main} thrown in /in/PlPTE on line 46
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
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.0.20, 7.1.0 - 7.1.20
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.5.24 - 5.5.35, 5.6.8 - 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.

preferences:
168.07 ms | 401 KiB | 179 Q