In this program, You will learn how to check number is palindrome or not in php
a number is a palindrome if the reversed number is equal to the original number. for example 121, 101, 12321 are palindrome numbers.
<!DOCTYPE html>
<html lang="en">
<head>
<title>palindrome program</title>
</head>
<body>
<form>
Enter the number <input type="number" name="palindrome">
<input type="submit" value="submit" name="click"></br>
</form>
</body>
</html>
<?php
if(isset($_GET["click"]))
{
$n=$_GET['palindrome'];
$copy=$n;
$rev=0;
while($n>0)
{
/*It will find the reverse of the input entered by the user.*/
$rev=$rev*10+$n%10;
$n=(int)($n/10);
}
// Compare the reverse of input with the temporary variable
if($copy==$rev)
{
echo $copy.' is palindrome number';
}
else
{
echo $copy.' is not palindrome number';
}
}
?>
a step-by-step explanation of the palindrome number program given below:
if( isset($_GET["click"])
will evaluate the true expression.n
. i.e $n =$_GET['palindrome']
n
into the copy
variable for later comparison.n
value becomes zero.$rev=$rev*10+$n%10;
n
by 10 and after dividing store the integer value in the same variable n
. $n=(int)($n/10);