Palindrome checking function I recently did a little task for fun that required me to create a class to determine if a word is a palindrome or not. I know this is quite simple, but I'd be interested to know if a real developer (I'm not a programmer) would approach the task in the same way, or if I make any amateur mistakes/do anything stupid.

My code passed the automated tests on the website, but I understand those aren't perfect.

class Palindrome
{
    public static function isPalindrome($word)
    {
        $word = strtolower($word);
        $wordLength = strlen($word);
        $wordSplitPoint = ceil($wordLength / 2);

        if ($wordLength % 2 == 0) {
            $firstHalf = substr($word, 0, $wordSplitPoint);
            $secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
        } else {
            $firstHalf = substr($word, 0, $wordSplitPoint-1);
            $secondHalf = substr($word, $wordSplitPoint, $wordSplitPoint);
        }

        $secondHalfReversed = strrev($secondHalf);

        if ($firstHalf == $secondHalfReversed) {
            return TRUE;
        } else {
            return FALSE;
        }
    }
}

echo Palindrome::isPalindrome('Deleveled');
Others have pointed out a fix you could make to your implementation.  But there is a way to write this which is much simpler.  That takes advantage of a simplified definition of palindrome: "a word which reads the same forwards and backwards".

Now I haven't done PHP in years, and don't have a test environment, so this might have issues, but the basic idea should be clear.

public static function isPalindrome($word)
{
    return $word == strrev($word);
}




Update

Based on suggestions in the comments, this would be nicer:

function isPalindrome($word) {
    $word = preg_replace('/[^a-zA-Z]/', '', $word);
    $word = strtolower($word);
    return $word == strrev($word);
}


That will now correctly identify "Level" (ignoring capitalization) and "Madam, I'm Adam." (ignoring spaces and punctuation.)