3v4l.org

run code in 300+ PHP versions simultaneously
<?php function high($input) { // Initialize: Convert the string to lowercase, and generate an array of the alphabet $input = strtolower($input); $alphabet = range('a', 'z'); $values = array_flip($alphabet); // Split the words into an array, and declare initial scores $words = explode(" ", $input); $highestScore = null; $highestIndex = null; // Iterate over each word foreach($words as $k=>$w) { // Calculate the score of the current word // The score is the position of the flipped alphabet array, or 0 if its not in the lsit $score = array_sum(array_map(function($v) use ($values) { return $values[$v] ?? 0; }, str_split($w))); // If the current score is higher than the previous highest, overwrite it if ($highestScore < $score) { $highestScore = $score; $highestIndex = $k; } } // Return the word at the index where the score was the highest // If there were no scores (if $input is empty), return a default message return $words[$highestIndex] ?? 'N/A - No words found'; } var_dump(high('man i need a taxi up to ubud'));
Output for 7.2.0 - 7.2.34, 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.18, 8.3.0 - 8.3.4, 8.3.6
string(4) "taxi"
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 string(4) "taxi"

preferences:
164.81 ms | 401 KiB | 181 Q