Check Palindrome Number in PHP
What you’ll learn
- What a palindrome number is (same digits forward and backward).
- How to reverse digits with
% 10and/ 10, then compare to the original. - A second program that prints every palindrome between
100and200, 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.
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
function isPalindrome(n):
original = n
reversed = 0
while n != 0:
reversed = reversed * 10 + (n mod 10)
n = floor(n / 10)
return original == reversedCheck one number
Classic interview structure: 121 reads 121 backward too.
<?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.
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
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
🔄 Input / output examples
Swap number in Example 1 or change the loop bounds in Example 2.
| Value | Palindrome? |
|---|---|
0 | Yes |
7 | Yes |
121 | Yes |
123 | No (321) |
Edge cases
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.
n < 10
Always palindromes for nonnegative n.
⏱️ Time and space complexity
| Operation | Time | Extra space |
|---|---|---|
isPalindrome(n) | O(d) digits | O(1) |
Range [a, b] | O((b-a+1) · d) worst case | O(1) |
Summary
- Core trick: reverse digits with
% 10and/ 10, compare to the saved original. - Ranges: reuse
isPalindromeinside aforloop. - Watch-outs: overflow on huge values; negatives need a separate plan.
The word palindrome also describes words like “radar” or “level”—for integers we only compare digits, so single-digit numbers like 7 always pass.
8 people found this page helpful
