3v4l.org

run code in 300+ PHP versions simultaneously
<?php function a($string, $length) { $string = substr($string, 0, $length); $string = substr($string, 0, strrpos($string, ' ')); return $string; } function b($string, $length) { $words = explode(' ', $string); $string = array_reduce($words, function ($current, $item) use ($length) { return mb_strlen($current) > $length ? $current : "$current$item "; }, ''); return $string; } $length = 40; $string = 'The fox flew over the moon. The cow did a big twist into the pool.'; var_dump(a($string, $length)); var_dump(b($string, $length));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0tqIa
function name:  (null)
number of ops:  17
compiled vars:  !0 = $length, !1 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   ASSIGN                                                   !0, 40
   18     1        ASSIGN                                                   !1, 'The+fox+flew+over+the+moon.+The+cow+did+a+big+twist+into+the+pool.'
   20     2        INIT_FCALL                                               'var_dump'
          3        INIT_FCALL                                               'a'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !0
          6        DO_FCALL                                      0  $4      
          7        SEND_VAR                                                 $4
          8        DO_ICALL                                                 
   23     9        INIT_FCALL                                               'var_dump'
         10        INIT_FCALL                                               'b'
         11        SEND_VAR                                                 !1
         12        SEND_VAR                                                 !0
         13        DO_FCALL                                      0  $6      
         14        SEND_VAR                                                 $6
         15        DO_ICALL                                                 
         16      > RETURN                                                   1

Function a:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0tqIa
function name:  a
number of ops:  20
compiled vars:  !0 = $string, !1 = $length
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    4     2        INIT_FCALL                                               'substr'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 0
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $2      
          7        ASSIGN                                                   !0, $2
    5     8        INIT_FCALL                                               'substr'
          9        SEND_VAR                                                 !0
         10        SEND_VAL                                                 0
         11        INIT_FCALL                                               'strrpos'
         12        SEND_VAR                                                 !0
         13        SEND_VAL                                                 '+'
         14        DO_ICALL                                         $4      
         15        SEND_VAR                                                 $4
         16        DO_ICALL                                         $5      
         17        ASSIGN                                                   !0, $5
    6    18      > RETURN                                                   !0
    7    19*     > RETURN                                                   null

End of function a

Function b:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0tqIa
function name:  b
number of ops:  17
compiled vars:  !0 = $string, !1 = $length, !2 = $words
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   10     2        INIT_FCALL                                               'explode'
          3        SEND_VAL                                                 '+'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $3      
          6        ASSIGN                                                   !2, $3
   11     7        INIT_FCALL                                               'array_reduce'
          8        SEND_VAR                                                 !2
          9        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F0tqIa%3A11%240'
         10        BIND_LEXICAL                                             ~5, !1
   13    11        SEND_VAL                                                 ~5
         12        SEND_VAL                                                 ''
         13        DO_ICALL                                         $6      
   11    14        ASSIGN                                                   !0, $6
   14    15      > RETURN                                                   !0
   15    16*     > RETURN                                                   null

End of function b

Function %00%7Bclosure%7D%2Fin%2F0tqIa%3A11%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0tqIa
function name:  {closure}
number of ops:  16
compiled vars:  !0 = $current, !1 = $item, !2 = $length
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
   12     3        INIT_FCALL                                               'mb_strlen'
          4        SEND_VAR                                                 !0
          5        DO_ICALL                                         $3      
          6        IS_SMALLER                                               !2, $3
          7      > JMPZ                                                     ~4, ->10
          8    >   QM_ASSIGN                                        ~5      !0
          9      > JMP                                                      ->14
         10    >   ROPE_INIT                                     3  ~7      !0
         11        ROPE_ADD                                      1  ~7      ~7, !1
         12        ROPE_END                                      2  ~6      ~7, '+'
         13        QM_ASSIGN                                        ~5      ~6
         14    > > RETURN                                                   ~5
   13    15*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F0tqIa%3A11%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.04 ms | 1418 KiB | 27 Q