3v4l.org

run code in 300+ 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>
          3        DO_FCALL                                      0  $0      
          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:  35
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    >   INIT_FCALL                                               'implode'
         28        SEND_VAR                                                 !2
         29        SEND_VAR                                                 !5
         30        DO_ICALL                                         $21     
         31        VERIFY_RETURN_TYPE                                       $21
         32      > RETURN                                                   $21
   17    33*       VERIFY_RETURN_TYPE                                       
         34*     > RETURN                                                   null

End of function randomstring

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
168.59 ms | 1403 KiB | 20 Q