Check Palindrome Number in PHP

Beginner
⏱️ 8 min read
📚 Updated: May 2026
🎯 2 Code Examples
Digit reversal

What you’ll learn

  • What a palindrome number is (same digits forward and backward).
  • How to reverse digits with % 10 and / 10, then compare to the original.
  • A second program that prints every palindrome between 100 and 200, plus a live preview.

Overview

Think of 121: front to back is “1-2-1,” and back to front is the same. The program peels off the last digit repeatedly, builds the reversed number, and checks equality.

Single check

Try 121 or 12321.

Range scan

Every palindrome from 100 to 200.

Live preview

Nonnegative integers only (matches the PHP examples here).

Prerequisites

while loops, integer division, and % for the last digit.

  • Basic PHP syntax, echo, and reusable functions.
  • Comfort reading multi-digit integers digit-by-digit.

The idea

Take 121. Chop the last digit (1), remember it. Chop again (2), shift what you remembered left and add. Chop again (1). You rebuilt 121—if that equals what you started with, it is a palindrome.

Same trick works for 9009, 5, or 0.

Live preview

Enter a nonnegative integer (same convention as the PHP samples). Reverses digits the same way as Example 1.

Try 121, 123, 0, or 7. Negative input is rejected here.

Live result
Press “Check palindrome.”

Algorithm

Goal: decide whether the decimal digits of n read the same forward and backward.

Remember original

You will change a working copy of n; keep the first value safe.

Reverse digits

Repeatedly: digit = n % 10, rev = rev * 10 + digit, n /= 10 until n == 0.

Compare

If rev == original, you have a palindrome.

📜 Pseudocode

Pseudocode
function isPalindrome(n):
    original = n
    reversed = 0
    while n != 0:
        reversed = reversed * 10 + (n mod 10)
        n = floor(n / 10)
    return original == reversed
1

Check one number

Classic interview structure: 121 reads 121 backward too.

php
<?php
function isPalindrome(int $number): bool
{
    $originalNumber = $number;
    $reversedNumber = 0;

    while ($number != 0) {
        $remainder = $number % 10;
        $reversedNumber = $reversedNumber * 10 + $remainder;
        $number = (int)($number / 10);
    }

    return $originalNumber === $reversedNumber;
}

$number = 121;

if (isPalindrome($number)) {
    echo $number . " is a palindrome number.\n";
} else {
    echo $number . " is not a palindrome number.\n";
}
?>

Explanation

The loop strips digits from number until it becomes 0. reversedNumber accumulates the flipped version; comparing with originalNumber finishes the test.

2

Palindromes from 100 to 200

Three-digit palindromes in this band look like aba: hundreds digit equals units digit. The loop prints each hit.

php
<?php
function isPalindrome(int $num): bool
{
    $originalNum = $num;
    $reversedNum = 0;

    while ($num != 0) {
        $remainder = $num % 10;
        $reversedNum = $reversedNum * 10 + $remainder;
        $num = (int)($num / 10);
    }

    return $originalNum === $reversedNum;
}

echo "Palindrome numbers in the range 100 to 200:\n";

for ($i = 100; $i <= 200; $i++) {
    if (isPalindrome($i)) {
        echo $i . " ";
    }
}

echo "\n";
?>

Explanation

Same helper as Example 1; the outer for walks every candidate in the inclusive range.

Notes

Large values. For very large inputs, reversedNumber * 10 can exceed integer limits on some runtimes; using a string approach can avoid arithmetic overflow concerns.

Alternatives. Converting to a string and comparing indices from both ends avoids arithmetic overflow but needs extra memory.

❓ FAQ

Write the digits on paper. Read them left-to-right and right-to-left. If both readings are the same (like 121 or 9009), the number is a palindrome.
Rebuilding the number backward gives you something easy to compare with the original using ==. That is the classic loop interview solution.
Yes. Digits like 3 or 8 read the same forward and backward.
Yes. It has one digit and reads the same either way.
These snippets assume nonnegative integers. Negatives need extra care because stripping digits with % behaves differently with signs in PHP.
Reversing an n-digit number costs O(number of digits), roughly O(log10 value) for positive integers. Scanning a range multiplies by how many integers you test.

🔄 Input / output examples

Swap number in Example 1 or change the loop bounds in Example 2.

ValuePalindrome?
0Yes
7Yes
121Yes
123No (321)

Edge cases

Trailing zeros

120 vs 21

Integer reversal drops trailing zeros on the right (they become leading zeros, which disappear). So 120 reverses to 21—not a palindrome unless you treat numbers with leading-zero strings.

Single digit

n < 10

Always palindromes for nonnegative n.

⏱️ Time and space complexity

OperationTimeExtra space
isPalindrome(n)O(d) digitsO(1)
Range [a, b]O((b-a+1) · d) worst caseO(1)

Summary

  • Core trick: reverse digits with % 10 and / 10, compare to the saved original.
  • Ranges: reuse isPalindrome inside a for loop.
  • Watch-outs: overflow on huge values; negatives need a separate plan.
Did you know?

The word palindrome also describes words like “radar” or “level”—for integers we only compare digits, so single-digit numbers like 7 always pass.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful