Problem Statement:
Write a PHP function that checks if a given string is a palindrome.
Solution Explanation:
- Convert the string to lowercase using
strtolower()
. - Remove spaces using
str_replace(" ", "", $str)
. - Reverse the string using
strrev()
. - Compare the original and reversed strings.
PHP Code:
<?php function isPalindrome($str) { $str = strtolower(str_replace(" ", "", $str)); // Convert to lowercase and remove spaces return $str === strrev($str); // Compare original with reversed } // Test cases echo isPalindrome("madam") ? "True\n" : "False\n"; // Output: True echo isPalindrome("racecar") ? "True\n" : "False\n"; // Output: True echo isPalindrome("hello") ? "True\n" : "False\n"; // Output: False echo isPalindrome("A man a plan a canal Panama") ? "True\n" : "False\n"; // Output: True ?>