PHP Palindrome Number Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is a Palindrome Number?

A palindrome number reads the same forwards and backwards. Classic examples: 121, 3443, and every one-digit value.

Remember
n is palindrome  ↔  reverse(digits of n) == n

Examples:
  121  → reverse 121  → yes
  123  → reverse 321  → no
    7  → reverse 7    → yes
   10  → reverse 01=1 → no
nReversePalindrome?
121121Yes
12211221Yes
123321No

How to Check a Palindrome Number in PHP

Four clear steps — then pick a math reverse or a string compare.

  1. Reject negatives for this beginner rule (optional: work with abs() if your problem allows it).
  2. Copy $n into a temporary variable.
  3. Build the reverse: repeatedly take $digit = $temp % 10, then $rev = $rev * 10 + $digit, then $temp = intdiv($temp, 10).
  4. Compare $rev with the original $n.
MethodIdeaTimeBest for
Digit reverseBuild $rev with % 10 / intdivO(d)Interviews (no string help)
String compareCompare text with strrevO(d)Quick, readable code
Range scanReuse the helper for each valueO(N d)Listing palindromes

Pseudocode

Pseudocode
function IsPalindrome(n):
    if n < 0:
        return false

    original = n
    rev = 0

    while n > 0:
        digit = n mod 10
        rev = rev * 10 + digit
        n = n / 10

    return rev == original

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Core ideaReverse digits, then compare
Digit peel$n % 10 then intdiv($n, 10)
Build reverse$rev = $rev * 10 + $digit
One digitAlways palindrome
NegativesUsually not (this page: false)
Classic checks121 yes; 123 no; 10 no

Live Preview

Enter an integer to see the reversed digits and the palindrome check.

Whole numbers only. Negatives count as not palindrome here. Tap a chip or type a value.

Result
n = 121
Reverse = 121
121 is a palindrome

Worked Walkthrough — n = 121

Peel digits from the right and push them onto the reverse value.

Calculation
original = 121
rev = 0

Step 1: digit = 1, rev = 0*10 + 1 = 1,   n = 12
Step 2: digit = 2, rev = 1*10 + 2 = 12,  n = 1
Step 3: digit = 1, rev = 12*10 + 1 = 121, n = 0

rev == original → 121 is a palindrome

PHP Programs

Start with the math reverse — interviewers usually want that. Then list palindromes in a range, and finish with a string compare.

Example 1 — Digit Reverse

Build the reverse integer with remainder and integer division, then compare.

PHP
<?php
function isPalindrome(int $n): bool {
    if ($n < 0) {
        return false;
    }

    $original = $n;
    $rev = 0;

    while ($n > 0) {
        $digit = $n % 10;
        $rev = $rev * 10 + $digit;
        $n = intdiv($n, 10);
    }

    return $rev === $original;
}

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

How It Works

  1. Negatives return false immediately under this page’s rule.
  2. Save $original because the loop will destroy the working copy of $n.
  3. Each step peels the last digit with $n % 10 and appends it to $rev.
  4. intdiv($n, 10) drops that digit so the loop moves leftward.
  5. When the loop ends, $rev === $original means the digits matched both ways.

Example 2 — Palindromes from 100 to 200

Reuse the helper to print every palindrome in a small band.

PHP
<?php
function isPalindrome(int $n): bool {
    if ($n < 0) {
        return false;
    }

    $original = $n;
    $rev = 0;

    while ($n > 0) {
        $rev = $rev * 10 + ($n % 10);
        $n = intdiv($n, 10);
    }

    return $rev === $original;
}

echo "Palindrome numbers from 100 to 200:\n";
$line = "";
for ($i = 100; $i <= 200; $i++) {
    if (isPalindrome($i)) {
        $line .= $i . " ";
    }
}
echo trim($line) . "\n";
?>

How It Works

  1. Loop $i from 100 to 200 inclusive.
  2. Call isPalindrome($i) for each value.
  3. Collect matches into one space-separated line.
  4. Three-digit palindromes look like aba — first digit equals last.
  5. Keeping the helper pure makes range listing easy.

Example 3 — String Compare

Cast to text and check that it equals its reverse with strrev.

PHP
<?php
function isPalindromeStr(int $n): bool {
    if ($n < 0) {
        return false;
    }

    $s = (string) $n;
    return $s === strrev($s);
}

echo isPalindromeStr(121) ? "true\n" : "false\n";
echo isPalindromeStr(123) ? "true\n" : "false\n";
echo isPalindromeStr(7) ? "true\n" : "false\n";
echo isPalindromeStr(-121) ? "true\n" : "false\n";
?>

How It Works

  1. Same negative rule as Example 1.
  2. (string) $n turns the number into digit characters.
  3. strrev($s) flips the text.
  4. Compare with === for exact character equality.
  5. Same answers as the math method; use digit reverse when you must avoid strings.

Edge Cases & Pitfalls

Check these before calling the solution done.

0–9

Single digits

Always palindromes. The reverse loop still works.

Negatives

Usually false

The minus sign breaks left-to-right equality. Confirm the problem’s rule.

Trailing 0

Like 10

Integer reverse drops leading zeros, so 10 becomes 1 — not a palindrome.

Division

Use intdiv

Prefer intdiv($n, 10) when peeling digits so you stay in integer land.

Time and Space Complexity

ProgramTimeExtra space
Digit reverseO(d)O(1)
Range of size NO(N d)O(1)
String compareO(d)O(d)

Here d is the digit count. Both single-check methods scan the digits once; the string method allocates character storage.

Key Takeaways

  • Definition: digits match forwards and backwards.
  • Loop: $rev = $rev * 10 + ($n % 10), then intdiv($n, 10).
  • Compare: $rev === $original (or string equals strrev).
  • Test set: 121 → yes; 123 → no; 7 → yes; 10 → no.

One line: reverse the digits and check equality with the original number.

Frequently Asked Questions

A number that reads the same forwards and backwards, such as 121 or 3443. Single-digit numbers are always palindromes.
Reverse the digits with % 10 and intdiv by 10, then compare with the original — or cast to a string and compare it with strrev.
Usually no for interview problems, because of the leading minus sign. This page treats n < 0 as not a palindrome.
Numbers like 10 reverse to 01, which is 1 as an integer, so they are not palindromes.
Both methods are O(d), where d is the number of digits. Extra space is O(1) for the math reverse and O(d) for the string method.

Next: Pascal’s Triangle

Build rows of binomial coefficients with nested loops.

Pascal’s triangle tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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