3v4l.org

run code in 300+ PHP versions simultaneously
<? $upc = "5901234123457"; //valid //"889296852094"; //"5901244123457"; //invalid //"889295852094"; echo isValidBarcode($upc) ? 1 : 0; function isValidBarcode($barcode) { $barcode = trim($barcode); $bcLen = strlen($barcode); //If value is not numeric, it is not valid. if (!is_numeric($barcode) && ($bcLen == 12 || $bcLen == 13)) return false; //set empty variables $even_sum = $odd_sum = 0; preg_match_all('/(\w)(\w)/',$barcode,$oddEven); $even_sum = array_sum($oddEven[1]); // If you have an odd number of chars in the barcode, this regex will not account for the last char. // If you have an even number of chars, you will still need to pop the last char off manually. if($bcLen % 2 === 0) array_pop($oddEven[2]); $odd_sum = array_sum($oddEven[2]); //Multiply even_sum by 3, and then add odd_sum to it. $total_sum = $even_sum * 3 + $odd_sum; //get highest multiple of 10 of total_sum. $next_ten = ceil($total_sum / 10) * 10; //Subtract highest multiple of 10 from total_sum to get check_digit. $check_digit = $next_ten - $total_sum; //if check_digit is equal to the last digit in the barcode, it is valid. return $check_digit == substr($barcode, $bcLen-1); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 7
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 8
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AgQ8a
function name:  (null)
number of ops:  10
compiled vars:  !0 = $upc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, '5901234123457'
    8     1        INIT_FCALL_BY_NAME                                       'isValidBarcode'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $2      
          4      > JMPZ                                                     $2, ->7
          5    >   QM_ASSIGN                                        ~3      1
          6      > JMP                                                      ->8
          7    >   QM_ASSIGN                                        ~3      0
          8    >   ECHO                                                     ~3
   41     9      > RETURN                                                   1

Function isvalidbarcode:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 12, Position 2 = 17
Branch analysis from position: 12
2 jumps found. (Code = 47) Position 1 = 14, Position 2 = 16
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 19
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 34, Position 2 = 38
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 38
Branch analysis from position: 16
Branch analysis from position: 17
filename:       /in/AgQ8a
function name:  isValidBarcode
number of ops:  62
compiled vars:  !0 = $barcode, !1 = $bcLen, !2 = $even_sum, !3 = $odd_sum, !4 = $oddEven, !5 = $total_sum, !6 = $next_ten, !7 = $check_digit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
   12     1        INIT_FCALL                                               'trim'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $8      
          4        ASSIGN                                                   !0, $8
   14     5        STRLEN                                           ~10     !0
          6        ASSIGN                                                   !1, ~10
   17     7        INIT_FCALL                                               'is_numeric'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $12     
         10        BOOL_NOT                                         ~13     $12
         11      > JMPZ_EX                                          ~13     ~13, ->17
         12    >   IS_EQUAL                                         ~14     !1, 12
         13      > JMPNZ_EX                                         ~14     ~14, ->16
         14    >   IS_EQUAL                                         ~15     !1, 13
         15        BOOL                                             ~14     ~15
         16    >   BOOL                                             ~13     ~14
         17    > > JMPZ                                                     ~13, ->19
         18    > > RETURN                                                   <false>
   20    19    >   ASSIGN                                           ~16     !3, 0
         20        ASSIGN                                                   !2, ~16
   22    21        INIT_FCALL                                               'preg_match_all'
         22        SEND_VAL                                                 '%2F%28%5Cw%29%28%5Cw%29%2F'
         23        SEND_VAR                                                 !0
         24        SEND_REF                                                 !4
         25        DO_ICALL                                                 
   23    26        INIT_FCALL                                               'array_sum'
         27        FETCH_DIM_R                                      ~19     !4, 1
         28        SEND_VAL                                                 ~19
         29        DO_ICALL                                         $20     
         30        ASSIGN                                                   !2, $20
   27    31        MOD                                              ~22     !1, 2
         32        IS_IDENTICAL                                             ~22, 0
         33      > JMPZ                                                     ~23, ->38
         34    >   INIT_FCALL                                               'array_pop'
         35        FETCH_DIM_W                                      $24     !4, 2
         36        SEND_REF                                                 $24
         37        DO_ICALL                                                 
   28    38    >   INIT_FCALL                                               'array_sum'
         39        FETCH_DIM_R                                      ~26     !4, 2
         40        SEND_VAL                                                 ~26
         41        DO_ICALL                                         $27     
         42        ASSIGN                                                   !3, $27
   31    43        MUL                                              ~29     !2, 3
         44        ADD                                              ~30     ~29, !3
         45        ASSIGN                                                   !5, ~30
   34    46        INIT_FCALL                                               'ceil'
         47        DIV                                              ~32     !5, 10
         48        SEND_VAL                                                 ~32
         49        DO_ICALL                                         $33     
         50        MUL                                              ~34     $33, 10
         51        ASSIGN                                                   !6, ~34
   37    52        SUB                                              ~36     !6, !5
         53        ASSIGN                                                   !7, ~36
   40    54        INIT_FCALL                                               'substr'
         55        SEND_VAR                                                 !0
         56        SUB                                              ~38     !1, 1
         57        SEND_VAL                                                 ~38
         58        DO_ICALL                                         $39     
         59        IS_EQUAL                                         ~40     !7, $39
         60      > RETURN                                                   ~40
   41    61*     > RETURN                                                   null

End of function isvalidbarcode

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
138.8 ms | 1011 KiB | 20 Q