3v4l.org

run code in 500+ PHP versions simultaneously
<?php function halfer(string $string): array { $result = []; $result['explode2'] = explode("\n", $string, 2); $result['preg_split2'] = preg_split("~\R+~", $string, 2, PREG_SPLIT_NO_EMPTY); $newlinePosition = strpos($string,"\n"); $result['substrs'] = [substr($string, 0, $newlinePosition), substr($string, $newlinePosition)]; $result['nlP'] = preg_split("~\n~" ,$string, $newlinePosition); return $result; } $strings = [ 'two newlines' => "1st line\n2nd line\n3rd line", 'leading newline' => "\n1st line\n2nd line\n3rd line", 'lone trailing newline' => "1st line\n", 'no newlines' => "1st line", 'double newlines' => "1st line\n\n2nd line", '\r\n' => "1st\r\n2nd", ]; foreach ($strings as $label => $string) { echo "$label\n"; echo json_encode(halfer($string), JSON_PRETTY_PRINT); echo "\n---\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 17
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 17
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/KlssN
function name:  (null)
number of ops:  19
compiled vars:  !0 = $strings, !1 = $string, !2 = $label
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   14     0  E >   ASSIGN                                                       !0, <array>
   23     1      > FE_RESET_R                                           $4      !0, ->17
          2    > > FE_FETCH_R                                           ~5      $4, !1, ->17
          3    >   ASSIGN                                                       !2, ~5
   24     4        NOP                                                          
          5        FAST_CONCAT                                          ~7      !2, '%0A'
          6        ECHO                                                         ~7
   25     7        INIT_FCALL                                                   'json_encode'
          8        INIT_FCALL                                                   'halfer'
          9        SEND_VAR                                                     !1
         10        DO_FCALL                                          0  $8      
         11        SEND_VAR                                                     $8
         12        SEND_VAL                                                     128
         13        DO_ICALL                                             $9      
         14        ECHO                                                         $9
   26    15        ECHO                                                         '%0A---%0A'
   23    16      > JMP                                                          ->2
         17    >   FE_FREE                                                      $4
   27    18      > RETURN                                                       1

Function halfer:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/KlssN
function name:  halfer
number of ops:  37
compiled vars:  !0 = $string, !1 = $result, !2 = $newlinePosition
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
    5     1        ASSIGN                                                       !1, <array>
    6     2        INIT_FCALL                                                   'explode'
          3        SEND_VAL                                                     '%0A'
          4        SEND_VAR                                                     !0
          5        SEND_VAL                                                     2
          6        DO_ICALL                                             $5      
          7        ASSIGN_DIM                                                   !1, 'explode2'
          8        OP_DATA                                                      $5
    7     9        INIT_FCALL                                                   'preg_split'
         10        SEND_VAL                                                     '%7E%5CR%2B%7E'
         11        SEND_VAR                                                     !0
         12        SEND_VAL                                                     2
         13        SEND_VAL                                                     1
         14        DO_ICALL                                             $7      
         15        ASSIGN_DIM                                                   !1, 'preg_split2'
         16        OP_DATA                                                      $7
    8    17        FRAMELESS_ICALL_2                strpos              ~8      !0, '%0A'
         18        ASSIGN                                                       !2, ~8
    9    19        FRAMELESS_ICALL_3                substr              ~11     !0, 0
         20        OP_DATA                                                      !2
         21        INIT_ARRAY                                           ~12     ~11
         22        FRAMELESS_ICALL_2                substr              ~13     !0, !2
         23        ADD_ARRAY_ELEMENT                                    ~12     ~13
         24        ASSIGN_DIM                                                   !1, 'substrs'
         25        OP_DATA                                                      ~12
   10    26        INIT_FCALL                                                   'preg_split'
         27        SEND_VAL                                                     '%7E%0A%7E'
         28        SEND_VAR                                                     !0
         29        SEND_VAR                                                     !2
         30        DO_ICALL                                             $15     
         31        ASSIGN_DIM                                                   !1, 'nlP'
         32        OP_DATA                                                      $15
   11    33        VERIFY_RETURN_TYPE                                           !1
         34      > RETURN                                                       !1
   12    35*       VERIFY_RETURN_TYPE                                           
         36*     > RETURN                                                       null

End of function halfer

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
211.65 ms | 2358 KiB | 17 Q