Definition
Same both ways
Digits match when read left-to-right and right-to-left.
A palindrome number reads the same forward and backward — like 121 or 9009. This tutorial covers digit reversal with % 10 and / 10, range listing, a string alternative, a live preview, worked PHP examples, edge cases, and complexity.
Same both ways
Digits match when read left-to-right and right-to-left.
% 10 / 10
Peel the last digit and rebuild the flipped number.
rev == original
Equality finishes the classic interview check.
100..200
Print every three-digit palindrome in the band.
Try 121
Check nonnegative integers in the browser.
d digits
One pass over the digits; ranges multiply that cost.
A palindrome number has decimal digits that read the same forward and backward. Think of 121: front to back is “1-2-1,” and back to front is the same.
The classic interview approach peels off the last digit repeatedly, builds the reversed number, and checks equality with the original. Single-digit values like 7 or 0 always pass.
It is a staple loop drill: modulo, integer division, and careful handling of trailing zeros and negatives.
You mutate a working copy while reversing.
rev = rev * 10 + n % 10
Always palindromes for nonnegative n.
120 reverses to 21 — not a palindrome.
In short: reverse the digits of a nonnegative integer and compare with the saved original — equality means palindrome.
Given a nonnegative integer, decide whether its decimal digits form a palindrome. Optionally list all palindromes in a closed range.
// 121 -> reverse 121 -> palindrome
// 123 -> reverse 321 -> not a palindrome
// 120 -> reverse 21 -> not a palindrome | Item | Type | Description |
|---|---|---|
$number | int | Nonnegative integer to classify. |
| Return | bool | true if digits form a palindrome. |
| Printed output | text | Yes/no sentence, or listed values in a range. |
function isPalindrome(n):
original = n
reversed = 0
while n != 0:
reversed = reversed * 10 + (n mod 10)
n = floor(n / 10)
return original == reversed | Method | Idea | Notes |
|---|---|---|
| Digit reverse | Build rev with % / 10 | Interview default — O(d) time, O(1) space |
| String two ends | Compare chars from both sides | Avoids overflow; uses O(d) string space |
| Half reverse | Reverse only half the digits | LeetCode-style optimization |
| Goal | Pattern |
|---|---|
| Last digit | $digit = $n % 10; |
| Drop last digit | $n = (int)($n / 10); |
| Append to reverse | $rev = $rev * 10 + $digit; |
| Compare | return $original === $rev; |
| Range filter | if (isPalindrome($i)) echo $i . " "; |
| String check | $s === strrev($s) |
Same answer — different costs and interview signals.
% / 10 loopThis page — classic O(1) extra space
strrevSimple; watch memory and leading zeros
stop mid-wayFewer multiplies for large values
mention 120Trailing zeros break integer reverse
Reach for digit reversal whenever you must test mirror symmetry of an integer.
Loops, modulo, and equality checks.
Same idea with characters instead of digits.
List all palindromes in a band for demos.
Practice % 10 and integer division.
Define a policy for negatives before coding.
Key benefit: one short loop that locks in digit extraction, reversal, and edge talk about zeros.
Enter a nonnegative integer (same convention as the PHP samples). Reverses digits the same way as Example 1.
Three complete PHP programs — reverse-and-compare for 121, palindromes from 100 to 200, and a string two-pointer check. Click View Output to reveal sample console results.
Classic reverse-digits helper with sample 121.
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";
}
?> The loop strips digits from $number until it becomes 0. $reversedNumber accumulates the flipped version; comparing with $originalNumber finishes the test.
Reuse the helper to list every hit in a closed band.
Three-digit palindromes in this band look like aba: hundreds digit equals units digit.
<?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";
?> Same helper as Example 1; the outer for walks every candidate in the inclusive range and prints hits.
Compare characters from both ends — no arithmetic reverse.
Convert to a string and walk inward from both ends. Useful when overflow is a concern.
<?php
function isPalindromeString(int $number): bool
{
if ($number < 0) {
return false;
}
$s = (string)$number;
$left = 0;
$right = strlen($s) - 1;
while ($left < $right) {
if ($s[$left] !== $s[$right]) {
return false;
}
$left++;
$right--;
}
return true;
}
$number = 12321;
echo $number . (isPalindromeString($number) ? " is a palindrome number.\n" : " is not a palindrome number.\n");
?> Indices $left and $right move toward the middle; any mismatched pair fails immediately. This uses O(d) string space but never multiplies a growing reverse integer.
Save $original before mutating the working copy.
Take % 10, append to $rev, then integer-divide by 10.
If $rev === $original, it is a palindrome.
Return true or false (or print a clear sentence).
121Trace the digit loop for Example 1.
| Step | Working $n | Digit | $rev |
|---|---|---|---|
| Start | 121 | — | 0 |
| 1 | 12 | 1 | 1 |
| 2 | 1 | 2 | 12 |
| 3 | 0 | 1 | 121 |
Final: 121 === 121 — palindrome.
Where integer palindrome checks show up beyond the interview prompt.
Digit loops and equality.
Example: write isPalindrome($n).
Print all hits in a closed band.
Example: 100..200 scan.
Last digit and drop-last patterns.
Example: peel 121 step by step.
Same mirror idea with characters.
Example: Example 3 pattern.
Huge reverses may need strings.
Example: mention int limits.
Explain why 120 fails as an integer.
Example: 120 → 21.
Pro Tip: open with “save original, reverse with % and /, compare” before writing the loop.
Why digit-reversal earns interview points.
Only a few integers — no array of digits required.
Dry-run 121 on paper and watch rev grow.
Drop isPalindrome into any range scan.
O(d) in the number of digits.
Pro Tip: lead with digit reverse; mention the string approach as a overflow-safe alternative.
Small habits that keep palindrome solutions interview-ready.
Never compare after you have zeroed the working copy without a backup.
Use (int)($n / 10) so PHP stays on whole digits.
State whether negatives are rejected or handled specially.
Dry-run 10 and 120 so you remember leading zeros vanish.
For huge values, discuss string comparison as a backup.
Pro Tip: dry-run 121 → rev 1, 12, 121 aloud before coding — if that matches, the update formula is correct.
Mistakes that commonly break palindrome-number solutions.
Comparing after the working value is already 0.
→ Store $original before the loop.
Using / without casting can leave fractions.
→ Use (int)($n / 10) (or intdiv).
Expecting 120 to reverse to 021.
→ Integers drop leading zeros; 120 → 21.
Feeding signed values into unsigned digit logic.
→ Reject or handle signs explicitly.
Growing $rev * 10 past platform limits.
→ Use a string check or discuss big integers.
Handle these before calling the check done.
120 vs 21Integer reversal drops trailing zeros on the right (they become leading zeros). So 120 reverses to 21.
n < 10Always palindromes for nonnegative n.
n = 0One digit — palindrome.
These samples assume nonnegative; reject or define a rule.
1221Works the same — reverse still equals original.
Consider string comparison if reverse may overflow.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
% 10 and / 10, compare to the saved original.isPalindrome inside a for loop.Quick Takeaway: reverse digits, compare to original — and remember trailing zeros break integer reverse.
| Operation | Time | Extra space |
|---|---|---|
isPalindrome(n) digit reverse | O(d) digits | O(1) |
| String two-pointer | O(d) | O(d) for the string |
Range [a, b] | O((b-a+1) · d) | O(1) |
For positive integers, d is about floor(log10 n) + 1.
Checking a palindrome number is digit reversal plus equality: save the original, rebuild with % 10 and / 10, and compare. Reuse the helper for ranges, and keep string comparison ready when overflow or negatives need a clearer story.
Practice the three examples above, then continue to Pascal’s triangle.
Save original, reverse digits, compare — and dry-run 120 so trailing zeros do not surprise you.
Check digit mirrors the interview-friendly way.
% 10 / 10
Patternrev == original
Rule1 digit OK
Edge120 fails
EdgeO(d) / O(1)
AnalysisThe word palindrome also describes words like “radar” or “level”—for integers we only compare digits, so single-digit numbers like 7 always pass.
Learn how to generate Pascal’s triangle rows with nested loops in PHP.
8 people found this page helpful