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 ($key === $str) { echo "Found $str!"; } } } $result = foo(array('ab', 'cd', '10')); bar($result, '10');
Output for 7.0.0 - 7.0.24, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Output for 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.30
Warning: Unsupported declare 'strict_types' in /in/7Ljsg on line 2 Parse error: syntax error, unexpected ':', expecting '{' in /in/7Ljsg on line 11
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected ':', expecting '{' in /in/7Ljsg on line 11
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_ARRAY, expecting '&' or T_VARIABLE in /in/7Ljsg on line 11
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_ARRAY, expecting ')' in /in/7Ljsg on line 11
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1
Parse error: parse error, unexpected T_ARRAY, expecting ')' in /in/7Ljsg on line 11
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/7Ljsg on line 11
Process exited with code 255.

preferences:
304.34 ms | 401 KiB | 387 Q