3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Returns the total number of $count-length strings generatable from $letters. function getPermCount($letters, $count) { $result = 1; // k characters from a set of n has n!/(n-k)! possible combinations for($i = strlen($letters) - $count + 1; $i <= strlen($letters); $i++) { $result *= $i; } return $result; } // Decodes $index to a $count-length string from $letters, no repeat chars. function getPerm($letters, $count, $index) { $result = ''; for($i = 0; $i < $count; $i++) { $pos = $index % strlen($letters); $result .= $letters[$pos]; $index = ($index-$pos)/strlen($letters); $letters = substr($letters, 0, $pos) . substr($letters, $pos+1); } return $result; } $letters = 'abcd'; echo '2 letters from 4:<br>'; for($i = 0; $i < getPermCount($letters, 2); $i++) echo getPerm($letters, 2, $i).'<br>'; echo '<br>3 letters from 4:<br>'; for($i = 0; $i < getPermCount($letters, 3); $i++) echo getPerm($letters, 3, $i). " ";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 4
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 35, Position 2 = 21
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 44) Position 1 = 35, Position 2 = 21
Branch analysis from position: 35
Branch analysis from position: 21
Branch analysis from position: 4
2 jumps found. (Code = 44) Position 1 = 18, Position 2 = 4
Branch analysis from position: 18
Branch analysis from position: 4
filename:       /in/ZjUop
function name:  (null)
number of ops:  36
compiled vars:  !0 = $letters, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   ASSIGN                                                   !0, 'abcd'
   28     1        ECHO                                                     '2+letters+from+4%3A%3Cbr%3E'
   29     2        ASSIGN                                                   !1, 0
          3      > JMP                                                      ->12
   30     4    >   INIT_FCALL                                               'getperm'
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 2
          7        SEND_VAR                                                 !1
          8        DO_FCALL                                      0  $4      
          9        CONCAT                                           ~5      $4, '%3Cbr%3E'
         10        ECHO                                                     ~5
   29    11        PRE_INC                                                  !1
         12    >   INIT_FCALL                                               'getpermcount'
         13        SEND_VAR                                                 !0
         14        SEND_VAL                                                 2
         15        DO_FCALL                                      0  $7      
         16        IS_SMALLER                                               !1, $7
         17      > JMPNZ                                                    ~8, ->4
   32    18    >   ECHO                                                     '%3Cbr%3E3+letters+from+4%3A%3Cbr%3E'
   33    19        ASSIGN                                                   !1, 0
         20      > JMP                                                      ->29
   34    21    >   INIT_FCALL                                               'getperm'
         22        SEND_VAR                                                 !0
         23        SEND_VAL                                                 3
         24        SEND_VAR                                                 !1
         25        DO_FCALL                                      0  $10     
         26        CONCAT                                           ~11     $10, '+'
         27        ECHO                                                     ~11
   33    28        PRE_INC                                                  !1
         29    >   INIT_FCALL                                               'getpermcount'
         30        SEND_VAR                                                 !0
         31        SEND_VAL                                                 3
         32        DO_FCALL                                      0  $13     
         33        IS_SMALLER                                               !1, $13
         34      > JMPNZ                                                    ~14, ->21
   34    35    > > RETURN                                                   1

Function getpermcount:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 8
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 13, Position 2 = 8
Branch analysis from position: 13
Branch analysis from position: 8
filename:       /in/ZjUop
function name:  getPermCount
number of ops:  15
compiled vars:  !0 = $letters, !1 = $count, !2 = $result, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    5     2        ASSIGN                                                   !2, 1
    7     3        STRLEN                                           ~5      !0
          4        SUB                                              ~6      ~5, !1
          5        ADD                                              ~7      ~6, 1
          6        ASSIGN                                                   !3, ~7
          7      > JMP                                                      ->10
    8     8    >   ASSIGN_OP                                     3          !2, !3
    7     9        PRE_INC                                                  !3
         10    >   STRLEN                                           ~11     !0
         11        IS_SMALLER_OR_EQUAL                                      !3, ~11
         12      > JMPNZ                                                    ~12, ->8
   10    13    > > RETURN                                                   !2
   11    14*     > RETURN                                                   null

End of function getpermcount

Function getperm:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 6
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 6
Branch analysis from position: 30
Branch analysis from position: 6
filename:       /in/ZjUop
function name:  getPerm
number of ops:  32
compiled vars:  !0 = $letters, !1 = $count, !2 = $index, !3 = $result, !4 = $i, !5 = $pos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   16     3        ASSIGN                                                   !3, ''
   17     4        ASSIGN                                                   !4, 0
          5      > JMP                                                      ->28
   19     6    >   STRLEN                                           ~8      !0
          7        MOD                                              ~9      !2, ~8
          8        ASSIGN                                                   !5, ~9
   20     9        FETCH_DIM_R                                      ~11     !0, !5
         10        ASSIGN_OP                                     8          !3, ~11
   21    11        SUB                                              ~13     !2, !5
         12        STRLEN                                           ~14     !0
         13        DIV                                              ~15     ~13, ~14
         14        ASSIGN                                                   !2, ~15
   22    15        INIT_FCALL                                               'substr'
         16        SEND_VAR                                                 !0
         17        SEND_VAL                                                 0
         18        SEND_VAR                                                 !5
         19        DO_ICALL                                         $17     
         20        INIT_FCALL                                               'substr'
         21        SEND_VAR                                                 !0
         22        ADD                                              ~18     !5, 1
         23        SEND_VAL                                                 ~18
         24        DO_ICALL                                         $19     
         25        CONCAT                                           ~20     $17, $19
         26        ASSIGN                                                   !0, ~20
   17    27        PRE_INC                                                  !4
         28    >   IS_SMALLER                                               !4, !1
         29      > JMPNZ                                                    ~23, ->6
   24    30    > > RETURN                                                   !3
   25    31*     > RETURN                                                   null

End of function getperm

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
152 ms | 1398 KiB | 19 Q