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) { array_walk($outputFromFoo, function ($value, $key) use ($str) { if ($key == $str) { echo "Found $str!"; } }); } $result = foo(array('ab', 'cd', '10')); bar($result, '10');
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rViVL
function name:  (null)
number of ops:  9
compiled vars:  !0 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   INIT_FCALL                                               'foo'
          1        SEND_VAL                                                 <array>
          2        DO_FCALL                                      0  $1      
          3        ASSIGN                                                   !0, $1
   36     4        INIT_FCALL                                               'bar'
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 '10'
          7        DO_FCALL                                      0          
          8      > RETURN                                                   1

Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rViVL
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
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/rViVL
function name:  bar
number of ops:  9
compiled vars:  !0 = $outputFromFoo, !1 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   28     2        INIT_FCALL                                               'array_walk'
          3        SEND_REF                                                 !0
          4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FrViVL%3A28%240'
          5        BIND_LEXICAL                                             ~2, !1
   32     6        SEND_VAL                                                 ~2
          7        DO_ICALL                                                 
   33     8      > RETURN                                                   null

End of function bar

Function %00%7Bclosure%7D%2Fin%2FrViVL%3A28%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/rViVL
function name:  {closure}
number of ops:  10
compiled vars:  !0 = $value, !1 = $key, !2 = $str
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
   29     3        IS_EQUAL                                                 !1, !2
          4      > JMPZ                                                     ~3, ->9
   30     5    >   ROPE_INIT                                     3  ~5      'Found+'
          6        ROPE_ADD                                      1  ~5      ~5, !2
          7        ROPE_END                                      2  ~4      ~5, '%21'
          8        ECHO                                                     ~4
   32     9    > > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FrViVL%3A28%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
157.83 ms | 1403 KiB | 21 Q