I have put together this Palindrome function in PHP
<?php
// example code
function isPalindrome($str){
// eliminate special chars
$str = preg_replace('/[^a-z0-9]+/i', '', $str);
// all to lowercase
$str = strtolower($str);
// reverse string
$strArr = str_split($str);
$strArr = array_reverse($strArr);
$reversed_str = join('',$strArr);
//Test
//echo $str . ' | ' . $reversed_str;
// compare
return $str === $reversed_str;
}
echo isPalindrome("A nut for a jar of tuna"); // returns 1
The problem: if the supplied string is a palindrome, the function returns 1, otherwise it returns nothing.
echo isPalindrome("A nut for a jar of fish"); // returns no output (in https://www.tehplayground.com)
I want it to return either true or false. Where is my mistake?