3v4l.org

run code in 300+ 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:  46
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        INIT_FCALL                                               'strpos'
         18        SEND_VAR                                                 !0
         19        SEND_VAL                                                 '%0A'
         20        DO_ICALL                                         $8      
         21        ASSIGN                                                   !2, $8
    9    22        INIT_FCALL                                               'substr'
         23        SEND_VAR                                                 !0
         24        SEND_VAL                                                 0
         25        SEND_VAR                                                 !2
         26        DO_ICALL                                         $11     
         27        INIT_ARRAY                                       ~12     $11
         28        INIT_FCALL                                               'substr'
         29        SEND_VAR                                                 !0
         30        SEND_VAR                                                 !2
         31        DO_ICALL                                         $13     
         32        ADD_ARRAY_ELEMENT                                ~12     $13
         33        ASSIGN_DIM                                               !1, 'substrs'
         34        OP_DATA                                                  ~12
   10    35        INIT_FCALL                                               'preg_split'
         36        SEND_VAL                                                 '%7E%0A%7E'
         37        SEND_VAR                                                 !0
         38        SEND_VAR                                                 !2
         39        DO_ICALL                                         $15     
         40        ASSIGN_DIM                                               !1, 'nlP'
         41        OP_DATA                                                  $15
   11    42        VERIFY_RETURN_TYPE                                       !1
         43      > RETURN                                                   !1
   12    44*       VERIFY_RETURN_TYPE                                       
         45*     > RETURN                                                   null

End of function halfer

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.87 ms | 1009 KiB | 19 Q