Skip to content

Instantly share code, notes, and snippets.

@JeremyMorgan
Last active August 29, 2015 14:03
Show Gist options
  • Select an option

  • Save JeremyMorgan/06106260495504cdb6c2 to your computer and use it in GitHub Desktop.

Select an option

Save JeremyMorgan/06106260495504cdb6c2 to your computer and use it in GitHub Desktop.
Check if a string is palindrome recursively
<?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