Skip to content

Instantly share code, notes, and snippets.

@jheth
Last active December 12, 2015 00:58
Show Gist options
  • Select an option

  • Save jheth/4687647 to your computer and use it in GitHub Desktop.

Select an option

Save jheth/4687647 to your computer and use it in GitHub Desktop.
PHP palindrome
<?php
$strings = array("racecar", "bob", "b", "abc", "car", "mom", "diary", null, '');
foreach ($strings as $str) {
if (palindrome($str)) {
print "$str is a palindrome\n";
} else {
print "$str is NOT a palindrome\n";
}
}
function palindrome($str)
{
if (!is_string($str)) {
return false;
}
$length = strlen($str);
for ($i = 0; $i < (int)($length / 2); $i++) {
if ($str[$i] != $str[$length-$i-1]) {
return false;
}
}
return true;
}
// cheat
function palindrome2($str)
{
return ($str == strrev($str));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment