3v4l.org

run code in 300+ PHP versions simultaneously
<?php class StringUtil{ /** * Returns the English plural of the given phrase based on the value. * * @see https://stackoverflow.com/a/34254595/1993494 (idea from this) * @param string $phrase The phrase to determine the plural form * @param float|int|array|object $value The value to determine whether the phrase should be plural or not * @return string The English plural form of the phrase based on the value */ public static function pluralEnglish(string $phrase, float|int|array|object $value): string{ if (empty($phrase) || is_numeric($phrase)){ return $phrase; } if (is_object($value) || is_array($value)){ $val = count($value); }else{ $val = $value; } if ($val == 1){ return $phrase; } //since the value is not singular, it is plural, so we need to come up with the english plural. $last = substr($phrase, -1); $last2 = substr($phrase, -2); $replace = false; $append = false; switch($last){ case 'x': case 'z': case 's': $append = 'es'; break; case 'y': $replace = 'ies'; break; default: switch($last2){ case 'ch': case 'sh': $append = 'es'; break; default: $append = 's'; } } if ($append) { return $phrase . $append; }else if ($replace){ return substr($phrase, 0, -1) . $replace; }else{ return $phrase; } } } $ary = [ 'kibitz', 'miss', 'fox', 'box', 'item', 'god', 'fun', 'diety', 'touch', 'teach', 'watch', 'test', 'fire', ]; $count = 0; foreach($ary as $phrase){ echo StringUtil::pluralEnglish($phrase, $count)."\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 11
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 11
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
filename:       /in/VJni0
function name:  (null)
number of ops:  13
compiled vars:  !0 = $ary, !1 = $count, !2 = $phrase
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   ASSIGN                                                   !0, <array>
   74     1        ASSIGN                                                   !1, 0
   75     2      > FE_RESET_R                                       $5      !0, ->11
          3    > > FE_FETCH_R                                               $5, !2, ->11
   76     4    >   INIT_STATIC_METHOD_CALL                                  'StringUtil', 'pluralEnglish'
          5        SEND_VAR                                                 !2
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0  $6      
          8        CONCAT                                           ~7      $6, '%0A'
          9        ECHO                                                     ~7
   75    10      > JMP                                                      ->3
         11    >   FE_FREE                                                  $5
   77    12      > RETURN                                                   1

Class StringUtil:
Function pluralenglish:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 11
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 47) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 19
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
6 jumps found. (Code = 188) Position 1 = 46, Position 2 = 46, Position 3 = 46, Position 4 = 48, Position 5 = 50, Position 6 = 37
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 64
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 64
2 jumps found. (Code = 43) Position 1 = 65, Position 2 = 74
Branch analysis from position: 65
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 74
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 46
Branch analysis from position: 46
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
Branch analysis from position: 50
4 jumps found. (Code = 188) Position 1 = 56, Position 2 = 56, Position 3 = 58, Position 4 = 51
Branch analysis from position: 56
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
Branch analysis from position: 56
Branch analysis from position: 58
2 jumps found. (Code = 43) Position 1 = 60, Position 2 = 64
Branch analysis from position: 60
Branch analysis from position: 64
Branch analysis from position: 51
2 jumps found. (Code = 44) Position 1 = 53, Position 2 = 56
Branch analysis from position: 53
2 jumps found. (Code = 44) Position 1 = 55, Position 2 = 56
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 58
Branch analysis from position: 58
Branch analysis from position: 56
Branch analysis from position: 56
Branch analysis from position: 37
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 46
Branch analysis from position: 39
2 jumps found. (Code = 44) Position 1 = 41, Position 2 = 46
Branch analysis from position: 41
2 jumps found. (Code = 44) Position 1 = 43, Position 2 = 46
Branch analysis from position: 43
2 jumps found. (Code = 44) Position 1 = 45, Position 2 = 48
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
Branch analysis from position: 48
Branch analysis from position: 46
Branch analysis from position: 46
Branch analysis from position: 46
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
Branch analysis from position: 24
Branch analysis from position: 15
Branch analysis from position: 8
filename:       /in/VJni0
function name:  pluralEnglish
number of ops:  78
compiled vars:  !0 = $phrase, !1 = $value, !2 = $val, !3 = $last, !4 = $last2, !5 = $replace, !6 = $append
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   12     2        ISSET_ISEMPTY_CV                                 ~7      !0
          3      > JMPNZ_EX                                         ~7      ~7, ->8
          4    >   INIT_FCALL                                               'is_numeric'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $8      
          7        BOOL                                             ~7      $8
          8    > > JMPZ                                                     ~7, ->11
   13     9    >   VERIFY_RETURN_TYPE                                       !0
         10      > RETURN                                                   !0
   16    11    >   TYPE_CHECK                                  256  ~9      !1
         12      > JMPNZ_EX                                         ~9      ~9, ->15
         13    >   TYPE_CHECK                                  128  ~10     !1
         14        BOOL                                             ~9      ~10
         15    > > JMPZ                                                     ~9, ->19
   17    16    >   COUNT                                            ~11     !1
         17        ASSIGN                                                   !2, ~11
   16    18      > JMP                                                      ->20
   19    19    >   ASSIGN                                                   !2, !1
   21    20    >   IS_EQUAL                                                 !2, 1
         21      > JMPZ                                                     ~14, ->24
   22    22    >   VERIFY_RETURN_TYPE                                       !0
         23      > RETURN                                                   !0
   26    24    >   INIT_FCALL                                               'substr'
         25        SEND_VAR                                                 !0
         26        SEND_VAL                                                 -1
         27        DO_ICALL                                         $15     
         28        ASSIGN                                                   !3, $15
   27    29        INIT_FCALL                                               'substr'
         30        SEND_VAR                                                 !0
         31        SEND_VAL                                                 -2
         32        DO_ICALL                                         $17     
         33        ASSIGN                                                   !4, $17
   28    34        ASSIGN                                                   !5, <false>
   29    35        ASSIGN                                                   !6, <false>
   30    36      > SWITCH_STRING                                            !3, [ 'x':->46, 'z':->46, 's':->46, 'y':->48, ], ->50
   31    37    >   IS_EQUAL                                                 !3, 'x'
         38      > JMPNZ                                                    ~21, ->46
   32    39    >   IS_EQUAL                                                 !3, 'z'
         40      > JMPNZ                                                    ~21, ->46
   33    41    >   IS_EQUAL                                                 !3, 's'
         42      > JMPNZ                                                    ~21, ->46
   36    43    >   IS_EQUAL                                                 !3, 'y'
         44      > JMPNZ                                                    ~21, ->48
         45    > > JMP                                                      ->50
   34    46    >   ASSIGN                                                   !6, 'es'
   35    47      > JMP                                                      ->59
   37    48    >   ASSIGN                                                   !5, 'ies'
   38    49      > JMP                                                      ->59
   40    50    > > SWITCH_STRING                                            !4, [ 'ch':->56, 'sh':->56, ], ->58
   41    51    >   IS_EQUAL                                                 !4, 'ch'
         52      > JMPNZ                                                    ~24, ->56
   42    53    >   IS_EQUAL                                                 !4, 'sh'
         54      > JMPNZ                                                    ~24, ->56
         55    > > JMP                                                      ->58
   43    56    >   ASSIGN                                                   !6, 'es'
   44    57      > JMP                                                      ->59
   46    58    >   ASSIGN                                                   !6, 's'
   49    59    > > JMPZ                                                     !6, ->64
   50    60    >   CONCAT                                           ~27     !0, !6
         61        VERIFY_RETURN_TYPE                                       ~27
         62      > RETURN                                                   ~27
   49    63*       JMP                                                      ->76
   51    64    > > JMPZ                                                     !5, ->74
   52    65    >   INIT_FCALL                                               'substr'
         66        SEND_VAR                                                 !0
         67        SEND_VAL                                                 0
         68        SEND_VAL                                                 -1
         69        DO_ICALL                                         $28     
         70        CONCAT                                           ~29     $28, !5
         71        VERIFY_RETURN_TYPE                                       ~29
         72      > RETURN                                                   ~29
   51    73*       JMP                                                      ->76
   54    74    >   VERIFY_RETURN_TYPE                                       !0
         75      > RETURN                                                   !0
   56    76*       VERIFY_RETURN_TYPE                                       
         77*     > RETURN                                                   null

End of function pluralenglish

End of class StringUtil.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.21 ms | 1010 KiB | 15 Q