3v4l.org

run code in 300+ PHP versions simultaneously
<?php function generateUpcCheckdigit($upc_code) { $odd_total = 0; $even_total = 0; for ($i = 1; $i <= strlen($upc_code); $i++) { //echo "Checking i=$i ({$upc_code[$i - 1]})...\n"; if ($i % 2 == 0) { //echo "even\n"; /* Sum even digits */ $even_total += $upc_code[$i - 1]; } else { //echo "odd\n"; /* Sum odd digits */ $odd_total += $upc_code[$i - 1]; } } $sum = (3 * $even_total) + $odd_total; //echo "Sum: $sum\n"; /* Get the remainder MOD 10*/ $check_digit = $sum % 10; /* If the result is not zero, subtract the result from ten. */ return ($check_digit > 0) ? 10 - $check_digit : $check_digit; } $coupons = [ [ 'signature' => 7685, 'description' => '10% off coupons', ], [ 'signature' => 7632, 'description' => '$15 off $50 coupons', ], ]; foreach ($coupons as $coupon) { echo "{$coupon['description']}:" . PHP_EOL; for ($i = 0; $i < 10; $i++) { $upc_code = '47000' . rand(0, 4) . rand(1000, 9999) . $coupon['signature']; echo $upc_code . generateUpcCheckdigit($upc_code) . PHP_EOL; } echo PHP_EOL; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 34
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 34
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 10
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 10
Branch analysis from position: 32
Branch analysis from position: 10
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
filename:       /in/lCo0X
function name:  (null)
number of ops:  36
compiled vars:  !0 = $coupons, !1 = $coupon, !2 = $i, !3 = $upc_code
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   ASSIGN                                                   !0, <array>
   36     1      > FE_RESET_R                                       $5      !0, ->34
          2    > > FE_FETCH_R                                               $5, !1, ->34
   37     3    >   FETCH_DIM_R                                      ~6      !1, 'description'
          4        NOP                                                      
          5        FAST_CONCAT                                      ~7      ~6, '%3A'
          6        CONCAT                                           ~8      ~7, '%0A'
          7        ECHO                                                     ~8
   38     8        ASSIGN                                                   !2, 0
          9      > JMP                                                      ->30
   39    10    >   INIT_FCALL                                               'rand'
         11        SEND_VAL                                                 0
         12        SEND_VAL                                                 4
         13        DO_ICALL                                         $10     
         14        CONCAT                                           ~11     '47000', $10
         15        INIT_FCALL                                               'rand'
         16        SEND_VAL                                                 1000
         17        SEND_VAL                                                 9999
         18        DO_ICALL                                         $12     
         19        CONCAT                                           ~13     ~11, $12
         20        FETCH_DIM_R                                      ~14     !1, 'signature'
         21        CONCAT                                           ~15     ~13, ~14
         22        ASSIGN                                                   !3, ~15
   40    23        INIT_FCALL                                               'generateupccheckdigit'
         24        SEND_VAR                                                 !3
         25        DO_FCALL                                      0  $17     
         26        CONCAT                                           ~18     !3, $17
         27        CONCAT                                           ~19     ~18, '%0A'
         28        ECHO                                                     ~19
   38    29        PRE_INC                                                  !2
         30    >   IS_SMALLER                                               !2, 10
         31      > JMPNZ                                                    ~21, ->10
   42    32    >   ECHO                                                     '%0A'
   36    33      > JMP                                                      ->2
         34    >   FE_FREE                                                  $5
   43    35      > RETURN                                                   1

Function generateupccheckdigit:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 5
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 29
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 30
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 15
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 5
Branch analysis from position: 19
Branch analysis from position: 5
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 5
Branch analysis from position: 19
Branch analysis from position: 5
filename:       /in/lCo0X
function name:  generateUpcCheckdigit
number of ops:  32
compiled vars:  !0 = $upc_code, !1 = $odd_total, !2 = $even_total, !3 = $i, !4 = $sum, !5 = $check_digit
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        ASSIGN                                                   !1, 0
    6     2        ASSIGN                                                   !2, 0
    7     3        ASSIGN                                                   !3, 1
          4      > JMP                                                      ->16
    9     5    >   MOD                                              ~9      !3, 2
          6        IS_EQUAL                                                 ~9, 0
          7      > JMPZ                                                     ~10, ->12
   12     8    >   SUB                                              ~11     !3, 1
          9        FETCH_DIM_R                                      ~12     !0, ~11
         10        ASSIGN_OP                                     1          !2, ~12
         11      > JMP                                                      ->15
   16    12    >   SUB                                              ~14     !3, 1
         13        FETCH_DIM_R                                      ~15     !0, ~14
         14        ASSIGN_OP                                     1          !1, ~15
    7    15    >   PRE_INC                                                  !3
         16    >   STRLEN                                           ~18     !0
         17        IS_SMALLER_OR_EQUAL                                      !3, ~18
         18      > JMPNZ                                                    ~19, ->5
   19    19    >   MUL                                              ~20     !2, 3
         20        ADD                                              ~21     ~20, !1
         21        ASSIGN                                                   !4, ~21
   22    22        MOD                                              ~23     !4, 10
         23        ASSIGN                                                   !5, ~23
   24    24        IS_SMALLER                                               0, !5
         25      > JMPZ                                                     ~25, ->29
         26    >   SUB                                              ~26     10, !5
         27        QM_ASSIGN                                        ~27     ~26
         28      > JMP                                                      ->30
         29    >   QM_ASSIGN                                        ~27     !5
         30    > > RETURN                                                   ~27
   25    31*     > RETURN                                                   null

End of function generateupccheckdigit

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
147.37 ms | 1403 KiB | 16 Q