Skip to content

Instantly share code, notes, and snippets.

@HoraceShmorace
Last active July 11, 2018 13:40
Show Gist options
  • Save HoraceShmorace/05cb97fb9cffaed807fc8c937aa02508 to your computer and use it in GitHub Desktop.
Save HoraceShmorace/05cb97fb9cffaed807fc8c937aa02508 to your computer and use it in GitHub Desktop.
Palindrome checker class
<?php // https://www.testdome.com/questions/php/palindrome/7320?questionIds=7320,11840&generatorId=30&type=fromtest&testDifficulty=Easy
class Palindrome {
public static function isPalindrome($word) {
$word = str_split(strtolower($word));
for($i = 0; $i < ceil(count($word)/2); $i++) {
$leftChar = $word[$i];
$rightChar = $word[count($word)-$i-1];
if($leftChar !== $rightChar) {
return FALSE;
}
}
return TRUE;
}
}
echo Palindrome::isPalindrome('Deleveled');
<?php // https://www.testdome.com/questions/php/palindrome/7320?questionIds=7320,11840&generatorId=30&type=fromtest&testDifficulty=Easy
class Palindrome
{
public static function isPalindrome($word) {
$word = strtolower($word);
return $word == strrev($word);
}
}
echo Palindrome::isPalindrome('Deleveled');
<?php // https://www.testdome.com/questions/php/palindrome/7320?questionIds=7320,11840&generatorId=30&type=fromtest&testDifficulty=Easy
class Palindrome {
public static function isPalindrome($word) {
$word = strtolower($word);
for($i = 0; $i < ceil(strlen($word)/2); $i++) {
$leftChar = substr($word, $i, 1);
$rightChar = substr($word, -($i+1), 1);
if($leftChar !== $rightChar) {
return FALSE;
}
}
return TRUE;
}
}
echo Palindrome::isPalindrome('Deleveled');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment