PHP program to check for palindrome number (using for loop)

Last updated:13th Aug 2022

In this example, you will check whether the number is palindrome or not by using for loop.121, 101, and 12321 are palindrome numbers.

When the original number and reverse of the original number are the same then the number is called a palindrome.


<?php
function ispalindrome($n){
    $copy=$n;
    $rev=0;
    for(; $n>0; $n=(int)($n/10))
    { 
        /*It will find the reverse of number.*/
        $rev=$rev*10+$n%10;
    }
    // Compare the reverse of number with the copy variable (original number)
    if($copy==$rev)
    {
        echo $copy.' is palindrome number';
    }
    else
    {
        echo $copy.' is not palindrome number';
    }
}
$n=121;
ispalindrome($n);
?>

output

121 is palindrome number