Palindrome Numbers in PHP

In PHP, determining whether a number is a palindrome involves checking if the number remains the same when its digits are reversed. Here’s a PHP example without using the strrev() function:

PHP Example:

By php program, without using strrev() function.

<?php
function isPalindrome($number)
{
    $originalNumber = $number;
    $reverse = 0;

    while (floor($number)) {
        $remainder = $number % 10;
        $reverse = $reverse * 10 + $remainder;
        $number = $number / 10;
    }

    return $reverse;
}

// Allow user input for dynamic palindrome checking.
$userInput = 12321;
$reversedNumber = isPalindrome($userInput);

if ($userInput == $reversedNumber) {
    echo "$userInput is a Palindrome number";
} else {
    echo "$userInput is not a Palindrome";
}
?>

Output:

The above PHP code will produce the following output:

12321 is a Palindrome number

Explanation:

  1. Function isPalindrome:
    • Takes a number as input and reverses its digits using a loop.
  2. Main Logic:
    • Compares the reversed number with the original to determine if it’s a palindrome.