Skip to content

Instantly share code, notes, and snippets.

@fintara
Last active August 29, 2015 13:56
Show Gist options
  • Save fintara/8960163 to your computer and use it in GitHub Desktop.
Save fintara/8960163 to your computer and use it in GitHub Desktop.
Three functions to check whether a number is a palindrome number.
<?php
function isPalindromA($num) {
$num = $num . '';
$len = strlen($num);
for($i = 0; $i < $len / 2; $i++)
if($num[$i] !== $num[$len - 1 - $i])
return false;
return true;
}
function isPalindromB($num) {
$num = $num . '';
$len = strlen($num);
if($len & 1) { // odd
$num = substr_replace($num, '', $len / 2, 1);
}
return substr($num, 0, strlen($num) / 2) == strrev(substr($num, strlen($num) / 2));
}
function isPalindromC($num) {
return $num == strrev($num);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment