3v4l.org

run code in 500+ PHP versions simultaneously
<?php // array of authors, number of authors to take first function niceAuthorsPrint(array $authors, $takeCount){ $totalAuthors = count( $authors ); if($totalAuthors >= 3 && $takeCount >= $totalAuthors) { $takeCount = 2; } if($totalAuthors == 2 && $takeCount >= $totalAuthors) { $takeCount = 1; } $last = array_pop($authors); $tailCount = $totalAuthors - $takeCount; $first = array_slice($authors, 0, $takeCount); $othersLabel = $tailCount == 1 ? $last : $tailCount . ' others'; $string = implode( ", ", $first ); if($tailCount > 0){ $string .= " and " . $othersLabel; } return $string; } // take 3 echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 3) ."\n"; // take 4 echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 4) ."\n"; // take 5 echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 5) ."\n"; // take original count, take first three as default echo niceAuthorsPrint(array( 'user1', 'user2', 'user3', 'user4', 'user5' ), 5) ."\n"; // take original count, take first three as default echo niceAuthorsPrint(array( 'user1', 'user2', 'user3'), 3) ."\n"; // take original count, take first three as default echo niceAuthorsPrint(array( 'user1', 'user2'), 2) ."\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/XB44q
function name:  (null)
number of ops:  37
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   36     0  E >   INIT_FCALL                                                   'niceauthorsprint'
          1        SEND_VAL                                                     <array>
          2        SEND_VAL                                                     3
          3        DO_FCALL                                          0  $0      
          4        CONCAT                                               ~1      $0, '%0A'
          5        ECHO                                                         ~1
   39     6        INIT_FCALL                                                   'niceauthorsprint'
          7        SEND_VAL                                                     <array>
          8        SEND_VAL                                                     4
          9        DO_FCALL                                          0  $2      
         10        CONCAT                                               ~3      $2, '%0A'
         11        ECHO                                                         ~3
   42    12        INIT_FCALL                                                   'niceauthorsprint'
         13        SEND_VAL                                                     <array>
         14        SEND_VAL                                                     5
         15        DO_FCALL                                          0  $4      
         16        CONCAT                                               ~5      $4, '%0A'
         17        ECHO                                                         ~5
   45    18        INIT_FCALL                                                   'niceauthorsprint'
         19        SEND_VAL                                                     <array>
         20        SEND_VAL                                                     5
         21        DO_FCALL                                          0  $6      
         22        CONCAT                                               ~7      $6, '%0A'
         23        ECHO                                                         ~7
   48    24        INIT_FCALL                                                   'niceauthorsprint'
         25        SEND_VAL                                                     <array>
         26        SEND_VAL                                                     3
         27        DO_FCALL                                          0  $8      
         28        CONCAT                                               ~9      $8, '%0A'
         29        ECHO                                                         ~9
   51    30        INIT_FCALL                                                   'niceauthorsprint'
         31        SEND_VAL                                                     <array>
         32        SEND_VAL                                                     2
         33        DO_FCALL                                          0  $10     
         34        CONCAT                                               ~11     $10, '%0A'
         35        ECHO                                                         ~11
         36      > RETURN                                                       1

Function niceauthorsprint:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
2 jumps found. (Code = 46) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 16
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 32
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 41
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 41
Branch analysis from position: 39
Branch analysis from position: 41
Branch analysis from position: 16
Branch analysis from position: 14
Branch analysis from position: 10
Branch analysis from position: 8
filename:       /in/XB44q
function name:  niceAuthorsPrint
number of ops:  43
compiled vars:  !0 = $authors, !1 = $takeCount, !2 = $totalAuthors, !3 = $last, !4 = $tailCount, !5 = $first, !6 = $othersLabel, !7 = $string
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    4     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
    6     2        COUNT                                                ~8      !0
          3        ASSIGN                                                       !2, ~8
    8     4        IS_SMALLER_OR_EQUAL                                  ~10     3, !2
          5      > JMPZ_EX                                              ~10     ~10, ->8
          6    >   IS_SMALLER_OR_EQUAL                                  ~11     !2, !1
          7        BOOL                                                 ~10     ~11
          8    > > JMPZ                                                         ~10, ->10
   10     9    >   ASSIGN                                                       !1, 2
   13    10    >   IS_EQUAL                                             ~13     !2, 2
         11      > JMPZ_EX                                              ~13     ~13, ->14
         12    >   IS_SMALLER_OR_EQUAL                                  ~14     !2, !1
         13        BOOL                                                 ~13     ~14
         14    > > JMPZ                                                         ~13, ->16
   15    15    >   ASSIGN                                                       !1, 1
   18    16    >   INIT_FCALL                                                   'array_pop'
         17        SEND_REF                                                     !0
         18        DO_ICALL                                             $16     
         19        ASSIGN                                                       !3, $16
   20    20        SUB                                                  ~18     !2, !1
         21        ASSIGN                                                       !4, ~18
   22    22        INIT_FCALL                                                   'array_slice'
         23        SEND_VAR                                                     !0
         24        SEND_VAL                                                     0
         25        SEND_VAR                                                     !1
         26        DO_ICALL                                             $20     
         27        ASSIGN                                                       !5, $20
   24    28        IS_EQUAL                                                     !4, 1
         29      > JMPZ                                                         ~22, ->32
         30    >   QM_ASSIGN                                            ~23     !3
         31      > JMP                                                          ->34
         32    >   CONCAT                                               ~24     !4, '+others'
         33        QM_ASSIGN                                            ~23     ~24
         34    >   ASSIGN                                                       !6, ~23
   26    35        FRAMELESS_ICALL_2                implode             ~26     '%2C+', !5
         36        ASSIGN                                                       !7, ~26
   28    37        IS_SMALLER                                                   0, !4
         38      > JMPZ                                                         ~28, ->41
   29    39    >   CONCAT                                               ~29     '+and+', !6
         40        ASSIGN_OP                                         8          !7, ~29
   32    41    > > RETURN                                                       !7
   33    42*     > RETURN                                                       null

End of function niceauthorsprint

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
165.32 ms | 2250 KiB | 21 Q