Last active
January 18, 2019 04:51
-
-
Save mirzalazuardi/b6da1188d1fd1d2042da39611b1757dc to your computer and use it in GitHub Desktop.
PHP #1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/**A palindrome is a word that reads the same backward or forward. Write a function that checks if a given word is a palindrome. | |
* Character case should be ignored. For example, isPalindrome("Deleveled") should return true as character case should be ignored, resulting in "deleveled", which is a palindrome since it reads the same backward and forward. | |
**/ | |
class Palindrome | |
{ | |
public static function isPalindrome($word) | |
{ | |
$word = trim($word); | |
$word = strtolower($word); | |
$chr_word = str_split($word); | |
$chr_word_rsv = array_reverse($chr_word); | |
return ($chr_word_rsv == $chr_word) ? true : false; | |
} | |
} | |
echo Palindrome::isPalindrome('level'); //palindrome |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment