3v4l.org

run code in 500+ PHP versions simultaneously
<?php function randomString(int $stringLength, array $parts, string $delimeter = ' ') : string { $keys = array_keys($parts); // Получаем массив ключей массива. Они упорядочены и идут с нулевого индекса. $count = count($keys); if (0 === $count) { return ''; } $text = []; for($i = 1; $i <= $stringLength; $i++) { // Больше нравится когда написано конкретно "с первого до последнего" $text[] = $parts[$keys[random_int(0, $count - 1)]]; // Получаем значение по случайному ключу из массива ключей } return implode($delimeter, $text); } echo randomString( 10, [ 'a' => 'lorem', 14 => 'ipsum', 'dolor' => 'dolor', 5 => 'sit', 'amet' => 'amet' ] );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G9JjW
function name:  (null)
number of ops:  6
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   19     0  E >   INIT_FCALL                                                   'randomstring'
   20     1        SEND_VAL                                                     10
   22     2        SEND_VAL                                                     <array>
   19     3        DO_FCALL                                          0  $0      
   22     4        ECHO                                                         $0
   28     5      > RETURN                                                       1

Function randomstring:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 12
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 15
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 15
Branch analysis from position: 27
Branch analysis from position: 15
filename:       /in/G9JjW
function name:  randomString
number of ops:  32
compiled vars:  !0 = $stringLength, !1 = $parts, !2 = $delimeter, !3 = $keys, !4 = $count, !5 = $text, !6 = $i
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV_INIT                                            !2      '+'
    4     3        INIT_FCALL                                                   'array_keys'
          4        SEND_VAR                                                     !1
          5        DO_ICALL                                             $7      
          6        ASSIGN                                                       !3, $7
    5     7        COUNT                                                ~9      !3
          8        ASSIGN                                                       !4, ~9
    7     9        IS_IDENTICAL                                                 !4, 0
         10      > JMPZ                                                         ~11, ->12
    8    11    > > RETURN                                                       ''
   11    12    >   ASSIGN                                                       !5, <array>
   12    13        ASSIGN                                                       !6, 1
         14      > JMP                                                          ->25
   13    15    >   INIT_FCALL                                                   'random_int'
         16        SEND_VAL                                                     0
         17        SUB                                                  ~15     !4, 1
         18        SEND_VAL                                                     ~15
         19        DO_ICALL                                             $16     
         20        FETCH_DIM_R                                          ~17     !3, $16
         21        FETCH_DIM_R                                          ~18     !1, ~17
         22        ASSIGN_DIM                                                   !5
         23        OP_DATA                                                      ~18
   12    24        PRE_INC                                                      !6
         25    >   IS_SMALLER_OR_EQUAL                                          !6, !0
         26      > JMPNZ                                                        ~20, ->15
   16    27    >   FRAMELESS_ICALL_2                implode             ~21     !2, !5
         28        VERIFY_RETURN_TYPE                                           ~21
         29      > RETURN                                                       ~21
   17    30*       VERIFY_RETURN_TYPE                                           
         31*     > RETURN                                                       null

End of function randomstring

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
158.01 ms | 1275 KiB | 16 Q