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 trim($result, ","); } $cars=array("a","b","c"); sort($cars); print_r($cars); $cars=array("a","c","b"); sort($cars); print_r($cars); $letters = 'abc'; $r = array(); //echo '1 letters from 4: '; for($i = 0; $i < getPermCount($letters, 1); $i++) $r[] = getPerm($letters, 1, $i); //echo '2 letters from 4: '; for($i = 0; $i < getPermCount($letters, 2); $i++) $r[] = getPerm($letters, 2, $i); //echo ' 3 letters from 4: '; for($i = 0; $i < getPermCount($letters, 3); $i++) $r[] = getPerm($letters, 3, $i); print_r($r);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 18
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 42
Branch analysis from position: 42
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 34
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 50
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 50
2 jumps found. (Code = 44) Position 1 = 64, Position 2 = 50
Branch analysis from position: 64
Branch analysis from position: 50
Branch analysis from position: 34
2 jumps found. (Code = 44) Position 1 = 48, Position 2 = 34
Branch analysis from position: 48
Branch analysis from position: 34
Branch analysis from position: 18
2 jumps found. (Code = 44) Position 1 = 32, Position 2 = 18
Branch analysis from position: 32
Branch analysis from position: 18
filename:       /in/lGHDc
function name:  (null)
number of ops:  68
compiled vars:  !0 = $cars, !1 = $letters, !2 = $r, !3 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   ASSIGN                                                   !0, <array>
   28     1        INIT_FCALL                                               'sort'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                                 
   29     4        INIT_FCALL                                               'print_r'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                                 
   31     7        ASSIGN                                                   !0, <array>
   32     8        INIT_FCALL                                               'sort'
          9        SEND_REF                                                 !0
         10        DO_ICALL                                                 
   33    11        INIT_FCALL                                               'print_r'
         12        SEND_VAR                                                 !0
         13        DO_ICALL                                                 
   35    14        ASSIGN                                                   !1, 'abc'
   36    15        ASSIGN                                                   !2, <array>
   39    16        ASSIGN                                                   !3, 0
         17      > JMP                                                      ->26
   40    18    >   INIT_FCALL                                               'getperm'
         19        SEND_VAR                                                 !1
         20        SEND_VAL                                                 1
         21        SEND_VAR                                                 !3
         22        DO_FCALL                                      0  $14     
         23        ASSIGN_DIM                                               !2
         24        OP_DATA                                                  $14
   39    25        PRE_INC                                                  !3
         26    >   INIT_FCALL                                               'getpermcount'
         27        SEND_VAR                                                 !1
         28        SEND_VAL                                                 1
         29        DO_FCALL                                      0  $16     
         30        IS_SMALLER                                               !3, $16
         31      > JMPNZ                                                    ~17, ->18
   43    32    >   ASSIGN                                                   !3, 0
         33      > JMP                                                      ->42
   44    34    >   INIT_FCALL                                               'getperm'
         35        SEND_VAR                                                 !1
         36        SEND_VAL                                                 2
         37        SEND_VAR                                                 !3
         38        DO_FCALL                                      0  $20     
         39        ASSIGN_DIM                                               !2
         40        OP_DATA                                                  $20
   43    41        PRE_INC                                                  !3
         42    >   INIT_FCALL                                               'getpermcount'
         43        SEND_VAR                                                 !1
         44        SEND_VAL                                                 2
         45        DO_FCALL                                      0  $22     
         46        IS_SMALLER                                               !3, $22
         47      > JMPNZ                                                    ~23, ->34
   47    48    >   ASSIGN                                                   !3, 0
         49      > JMP                                                      ->58
   48    50    >   INIT_FCALL                                               'getperm'
         51        SEND_VAR                                                 !1
         52        SEND_VAL                                                 3
         53        SEND_VAR                                                 !3
         54        DO_FCALL                                      0  $26     
         55        ASSIGN_DIM                                               !2
         56        OP_DATA                                                  $26
   47    57        PRE_INC                                                  !3
         58    >   INIT_FCALL                                               'getpermcount'
         59        SEND_VAR                                                 !1
         60        SEND_VAL                                                 3
         61        DO_FCALL                                      0  $28     
         62        IS_SMALLER                                               !3, $28
         63      > JMPNZ                                                    ~29, ->50
   50    64    >   INIT_FCALL                                               'print_r'
         65        SEND_VAR                                                 !2
         66        DO_ICALL                                                 
         67      > 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/lGHDc
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 = 29
Branch analysis from position: 29
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 6
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 31, Position 2 = 6
Branch analysis from position: 31
Branch analysis from position: 6
filename:       /in/lGHDc
function name:  getPerm
number of ops:  37
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                                                      ->29
   19     6    >   STRLEN                                           ~8      !0
          7        MOD                                              ~9      !2, ~8
          8        ASSIGN                                                   !5, ~9
   20     9        FETCH_DIM_R                                      ~11     !0, !5
         10        CONCAT                                           ~12     '%2C', ~11
         11        ASSIGN_OP                                     8          !3, ~12
   21    12        SUB                                              ~14     !2, !5
         13        STRLEN                                           ~15     !0
         14        DIV                                              ~16     ~14, ~15
         15        ASSIGN                                                   !2, ~16
   22    16        INIT_FCALL                                               'substr'
         17        SEND_VAR                                                 !0
         18        SEND_VAL                                                 0
         19        SEND_VAR                                                 !5
         20        DO_ICALL                                         $18     
         21        INIT_FCALL                                               'substr'
         22        SEND_VAR                                                 !0
         23        ADD                                              ~19     !5, 1
         24        SEND_VAL                                                 ~19
         25        DO_ICALL                                         $20     
         26        CONCAT                                           ~21     $18, $20
         27        ASSIGN                                                   !0, ~21
   17    28        PRE_INC                                                  !4
         29    >   IS_SMALLER                                               !4, !1
         30      > JMPNZ                                                    ~24, ->6
   24    31    >   INIT_FCALL                                               'trim'
         32        SEND_VAR                                                 !3
         33        SEND_VAL                                                 '%2C'
         34        DO_ICALL                                         $25     
         35      > RETURN                                                   $25
   25    36*     > RETURN                                                   null

End of function getperm

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.73 ms | 1411 KiB | 27 Q