3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); /** * Write a function that accepts an array of strings and return another array * with strings in the input array set as keys. * * @param string[] $arrOfStrings An array of strings. * @return array Another array with strings in the input array set as keys. */ function foo(array $arrOfStrings): array { return array_fill_keys(array_filter($arrOfStrings, 'is_string'), null); } /** * Now write another function that accepts the output of the previous function, * as first argument and another string as second argument. In this function, * assume that you have some reason to iterate over the the input array using * foreach, key => value format. In every iteration, in addition to other stuff, * you also have to check if the key is equal to the input string (the second * parameter of this function), and if they match do some stuff.... * * @param array $outputFromFoo The output from {@link foo()}. */ function bar(array $outputFromFoo, string $str) { foreach ($outputFromFoo as $key => $value) { // other stuff if ((string) $key === $str) { return true; } } return false; } $result = foo(array('ab', 'cd', '10')); var_dump(bar($result, 'cd')); var_dump($result);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6lsar
function name:  (null)
number of ops:  15
compiled vars:  !0 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   INIT_FCALL                                               'foo'
          1        SEND_VAL                                                 <array>
          2        DO_FCALL                                      0  $1      
          3        ASSIGN                                                   !0, $1
   39     4        INIT_FCALL                                               'var_dump'
          5        INIT_FCALL                                               'bar'
          6        SEND_VAR                                                 !0
          7        SEND_VAL                                                 'cd'
          8        DO_FCALL                                      0  $3      
          9        SEND_VAR                                                 $3
         10        DO_ICALL                                                 
   40    11        INIT_FCALL                                               'var_dump'
         12        SEND_VAR                                                 !0
         13        DO_ICALL                                                 
         14      > RETURN                                                   1

Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6lsar
function name:  foo
number of ops:  13
compiled vars:  !0 = $arrOfStrings
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
   13     1        INIT_FCALL                                               'array_fill_keys'
          2        INIT_FCALL                                               'array_filter'
          3        SEND_VAR                                                 !0
          4        SEND_VAL                                                 'is_string'
          5        DO_ICALL                                         $1      
          6        SEND_VAR                                                 $1
          7        SEND_VAL                                                 null
          8        DO_ICALL                                         $2      
          9        VERIFY_RETURN_TYPE                                       $2
         10      > RETURN                                                   $2
   14    11*       VERIFY_RETURN_TYPE                                       
         12*     > RETURN                                                   null

End of function foo

Function bar:
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
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 10
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
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/6lsar
function name:  bar
number of ops:  14
compiled vars:  !0 = $outputFromFoo, !1 = $str, !2 = $value, !3 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   28     2      > FE_RESET_R                                       $4      !0, ->11
          3    > > FE_FETCH_R                                       ~5      $4, !2, ->11
          4    >   ASSIGN                                                   !3, ~5
   31     5        CAST                                          6  ~7      !3
          6        IS_IDENTICAL                                             !1, ~7
          7      > JMPZ                                                     ~8, ->10
   32     8    >   FE_FREE                                                  $4
          9      > RETURN                                                   <true>
   28    10    > > JMP                                                      ->3
         11    >   FE_FREE                                                  $4
   35    12      > RETURN                                                   <false>
   36    13*     > RETURN                                                   null

End of function bar

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
192.02 ms | 1402 KiB | 21 Q