I need to check if a number is palindrome, i started thinking maybe the best way to do it, is transforming the number into a string and verifying the reverse string is the same.
The problem is that when i use the following code i got the wrong result.
<?PHP
function palindrome($number){
$value = strval($number);
$reverse_value = strrev($value);
if($value == strrev($value)){
echo " $number is a palindrome";
echo gettype($value);
}else{
echo "$number is not a palindrome";
echo $value." ".strrev($value);
}
}
$number = 90209;
palindrome($number);
?>
Can somebody explain me the diference?
It seems that you have deeper problems, because I just put that code into an ide:
https://ideone.com/yJEz1
Result:
90209 is a palindromestringAnd it successfully put out the value. You may want to check your php.ini with regard to changed mathematical settings, or just run the code again in a different context, or clean up duplication in the code, and it may give you the right result.
Here is the same code using
(string)casting to ensure the string type (I’ve never used the strval() function, personally, I just force cast a type). It works in all of three test cases.https://ideone.com/4bI0O