3v4l.org

run code in 300+ PHP versions simultaneously
<?php function isPalindrome(string &$str): bool { if ($str === '') { return false; // an empty string is not a palindrome } if (strlen($str) === 1) { return true; // odd length string is a palindrome, do not recurse } if ($str[0] !== $str[strlen($str) - 1]) { return false; // outermost characters are not identical, halt processing } $str = substr($str, 1, -1); // shed outermost charcters if ($str === '') { return true; // even length string is a palindrome, do not recurse } return (__FUNCTION__)($str); // traverse deeper and bubble outcome to top level } function sanitizePalindrome(&$str) { return preg_replace('/[^a-z]+/', '', strtolower($str)); } $tests = [ '', 'radar', 'f', 'neveroddoreven', 'foo', 'palindrome', 'Red rum, sir, is murder' ]; foreach ($tests as $test) { $test = sanitizePalindrome($test); // this is optional printf("%20s : %s\n", $test, json_encode(isPalindrome($test))); }

preferences:
13.49 ms | 408 KiB | 5 Q