Last active
August 29, 2015 13:56
-
-
Save fintara/8960163 to your computer and use it in GitHub Desktop.
Three functions to check whether a number is a palindrome number.
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 | |
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