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');
isPalindrome
function \$\endgroup\$