run code in 200+ php & hhvm versions
Bugs & Features
<?php /** * 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) { 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 ($key === $str) { echo "Found $str!"; } } } $result = foo(array('ab', 'cd', '10')); bar($result, '10');
based on BCi9E
Output for 7.0.0 - 7.2.0
Output for 5.3.22 - 5.6.28
Catchable fatal error: Argument 2 passed to bar() must be an instance of string, string given, called in /in/Fm0dQ on line 37 and defined in /in/Fm0dQ on line 25
Process exited with code 255.