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

Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/EuHVl
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 = 15
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 15
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 14
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 14
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/EuHVl
function name:  bar
number of ops:  17
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, ->15
          3    > > FE_FETCH_R                                       ~5      $4, !2, ->15
          4    >   ASSIGN                                                   !3, ~5
   30     5        INIT_FCALL                                               'print_r'
          6        SEND_VAR                                                 !3
          7        DO_ICALL                                                 
   31     8        IS_IDENTICAL                                             !3, !1
          9      > JMPZ                                                     ~8, ->14
   32    10    >   ROPE_INIT                                     3  ~10     'Found+'
         11        ROPE_ADD                                      1  ~10     ~10, !1
         12        ROPE_END                                      2  ~9      ~10, '%21'
         13        ECHO                                                     ~9
   28    14    > > JMP                                                      ->3
         15    >   FE_FREE                                                  $4
   35    16      > RETURN                                                   null

End of function bar

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.92 ms | 1403 KiB | 20 Q