Created
August 30, 2017 09:25
-
-
Save sela/af2373084e1dc397d39890c4e5888771 to your computer and use it in GitHub Desktop.
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 | |
class palindrome | |
{ | |
private static $caseSensitive = false; | |
static function isPalindrome($word) | |
{ | |
$reversedWord = strrev($word); | |
if (!self::$caseSensitive) { | |
$word = strtolower($word); // I assume we don't deal with unicode, if we do use mb_strtolower() | |
$reversedWord = strtolower($reversedWord); | |
} | |
if ($word === $reversedWord) { | |
return true; | |
} | |
return false; | |
} | |
static function setCaseSensitive() | |
{ | |
self::$caseSensitive = true; | |
} | |
static function setCaseInsensitive() | |
{ | |
self::$caseSensitive = false; | |
} | |
} | |
var_dump('anna', palindrome::isPalindrome('anna')); | |
var_dump('annb', palindrome::isPalindrome('annb')); | |
var_dump('Anna', palindrome::isPalindrome('Anna')); | |
palindrome::setCaseSensitive(); | |
var_dump('Anna', palindrome::isPalindrome('Anna')); | |
var_dump('civic', palindrome::isPalindrome('civic')); | |
var_dump('civia', palindrome::isPalindrome('aivic')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment