Last active
August 29, 2015 14:03
-
-
Save JeremyMorgan/06106260495504cdb6c2 to your computer and use it in GitHub Desktop.
Check if a string is palindrome recursively
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 | |
| // check if a string is a palindrome from the command line | |
| $inputstring = $argv[1]; | |
| function isPalindrome($inputstring) | |
| { | |
| if (strlen($inputstring) <= 1){ | |
| // if there is only one or zero characters, technically it's a palindrome | |
| return true; | |
| } | |
| else { | |
| if (substr($inputstring,0,1) == substr($inputstring,(strlen($inputstring) - 1),1)) { | |
| return isPalindrome(substr($inputstring,1,strlen($inputstring) -2)); | |
| } | |
| else { | |
| return false; | |
| } | |
| } | |
| } | |
| if (isPalindrome($inputstring) == true){ | |
| echo "\n\nCongrats you've got yourself a palindrome.\n\n"; | |
| }else { | |
| echo "\n\nSorry, This is not a palindrome\n\n"; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment