How to Check a Palindrome Number in PHP?

A palindrome number is a number that remains the same when its digits are reversed, like 121 or 1331.
In PHP, you can quickly check it using:

if($num == strrev($num)) echo "Palindrome";

A palindrome number is a number that reads the same forward and backward. For example, numbers like 121, 1221, and 1331 are palindromes, whereas 123 is not.

In coding, palindrome checks are a classic problem often asked in interviews to test logical thinking, string manipulation, and loop handling skills. Beyond interviews, palindrome logic is useful in data validation, error detection, and algorithm design where symmetry plays a role.

🔹 Quick Summary

Term / ConceptExplanation / Example
DefinitionA number that remains the same when reversed.
Examples of Palindrome121, 1331, 1221, 12321
Non-Palindrome Examples123, 456, 987
Quick PHP Approachif($num == strrev($num)) echo "Palindrome";

What is a Palindrome Number in PHP?

A Palindrome Number in PHP is a number that remains unchanged when its digits are reversed. In simple terms, it reads the same forward and backward.

For example:

  • 121 → Reversing gives 121, so it is a palindrome.
  • 1331 → Reversing gives 1331, still the same, hence a palindrome.
  • 123 → Reversing gives 321, which is not the same, so it is not a palindrome.

Checking for palindrome numbers helps programmers practice logical problem-solving and string manipulation in PHP.

PHP Program to Check Palindrome Number

Example Code:

<?php
$num = 121;  
$reverse = strrev($num);  

if ($num == $reverse) {  
    echo "$num is a Palindrome Number in PHP";  
} else {  
    echo "$num is not a Palindrome Number in PHP";  
}
?>

Output:

121 is a Palindrome Number in PHP

Step-by-Step Explanation of the Logic:

  1. Store the number – We take a number, for example 121.
  2. Reverse the number – Using the built-in PHP function strrev(), we reverse the number. For 121, the reverse is still 121.
  3. Compare original with reverse – If the original number and reversed number are the same, it is a Palindrome Number in PHP.
  4. Decision making – Using an if-else condition, the program prints whether the given number is palindrome or not.

Alternative Approach to Check Palindrome Number in PHP

While the simplest way to check a Palindrome Number in PHP is by using the strrev() function, we can also solve it using basic arithmetic operations like modulus and loops. This approach is often preferred in coding interviews because it shows logical problem-solving skills without relying only on built-in functions.

Example Code (Using Arithmetic Logic):

<?php
$num = 1331;  
$temp = $num;  
$reverse = 0;  

while ($num > 0) {  
    $digit = $num % 10;        // Extract last digit  
    $reverse = ($reverse * 10) + $digit;  
    $num = (int)($num / 10);   // Remove last digit  
}  

if ($temp == $reverse) {  
    echo "$temp is a Palindrome Number in PHP";  
} else {  
    echo "$temp is not a Palindrome Number in PHP";  
}
?>

Output:

1331 is a Palindrome Number in PHP

Explanation:

  1. Start with a number (1331).
  2. Extract its digits one by one using modulus (% 10).
  3. Rebuild the number in reverse order.
  4. Compare the reversed number with the original.
  5. If both match, it confirms the number is a palindrome.

Comparison of Approaches

ApproachProsCons
String-based (strrev)Simple, quick, requires minimal code.Relies on built-in functions, less logical depth.
Logic-based (Arithmetic)Good for interviews, shows understanding of algorithms.Slightly longer code, requires more explanation.

Both approaches are correct. For quick coding tasks, the strrev() method is more convenient, while for interviews or when built-in functions are restricted, the arithmetic method is the best way to check a Palindrome Number in PHP.

Real-Life Uses of Palindrome Numbers in PHP

Palindrome checking is not just a coding exercise; it has real-world applications across industries. Many organizations use palindrome logic in data validation, security, and pattern recognition. Below are some examples:

1. Payment Gateways (PayPal, Stripe)

  • Use Case: Transaction IDs are often checked for symmetry or uniqueness. Palindrome logic can be used to validate or generate test transaction IDs.
  • Example Code:
$transactionId = 1221;  
if ($transactionId == strrev($transactionId)) {  
    echo "Valid Test Transaction ID";  
}
  • Output:
Valid Test Transaction ID

2. Search Engines (Google, Bing)

  • Use Case: Palindrome detection is used in text analysis and keyword searches where symmetry matters (like “madam” or “level”).
  • Example Code:
$keyword = "madam";  
if ($keyword == strrev($keyword)) {  
    echo "Keyword is a Palindrome";  
}
  • Output:
Keyword is a Palindrome

3. Healthcare Data Systems (IBM Watson Health, Philips Healthcare)

  • Use Case: Patient IDs and lab sample numbers sometimes use palindrome checks to reduce input errors during data entry.
  • Example Code:
$patientId = 4554;  
if ($patientId == strrev($patientId)) {  
    echo "Patient ID Verified";  
}
  • Output:
Patient ID Verified

4. E-commerce Platforms (Amazon, Flipkart)

  • Use Case: Order IDs and coupon codes are validated for symmetry or uniqueness using palindrome checks. This helps during testing and debugging.
  • Example Code:
$couponCode = "1221";  
if ($couponCode == strrev($couponCode)) {  
    echo "Valid Palindrome Coupon";  
}
  • Output:
Valid Palindrome Coupon

5. Social Media Platforms (Twitter, Instagram)

  • Use Case: Palindrome detection is applied in fun features like identifying special usernames or trending palindrome hashtags (e.g., #12121).
  • Example Code:
$username = "12121";  
if ($username == strrev($username)) {  
    echo "Special Palindrome Username";  
}
  • Output:
Special Palindrome Username

Our AI-powered php online compiler offers a seamless coding experience. Instantly write, run, and test your ‘php’ code with the help of AI. It’s designed to streamline your coding process, making it quicker and more efficient. Try it out, and see the difference AI can make!

Palindrome Number in PHP Questions

1. How to check a Palindrome Number in PHP without converting to string or using strrev, and what is the most efficient way?

Most tutorials stop at strrev() or string conversion. But in interviews, you are often asked to solve it mathematically. Instead of reversing the whole number, you can reverse only half of it and compare, which saves time and memory.

Efficient PHP Example (Half-Reverse Approach):
<?php
function isPalindromeNumber($num) {
    if ($num < 0 || ($num % 10 == 0 && $num != 0)) return false;  

    $reverseHalf = 0;
    while ($num > $reverseHalf) {
        $reverseHalf = ($reverseHalf * 10) + ($num % 10);
        $num = intdiv($num, 10);
    }
    return ($num == $reverseHalf || $num == intdiv($reverseHalf, 10));
}

echo isPalindromeNumber(1221) ? "Palindrome" : "Not Palindrome";
?>

Output:

Palindrome

Why this matters:

  • You stop once half is reversed.
  • Efficient for large numbers.
  • Avoids memory waste of full reversal.
2. How does PHP behave with very large numbers (beyond typical int size) when checking a Palindrome Number?

In PHP, integers are platform-dependent (64-bit systems handle up to PHP_INT_MAX = 9223372036854775807). For numbers larger than this, PHP automatically treats them as float, which introduces precision issues.

Example:

$num = 12345678987654321; // Larger than 64-bit integer
echo ($num == strrev($num)) ? "Palindrome" : "Not Palindrome";

This won’t work correctly because $num is not preserved accurately as an integer.

Correct Approach:

  • Store the number as a string when extremely large.
$num = "12345678987654321";  
if ($num === strrev($num)) {
    echo "Palindrome";  
} else {
    echo "Not Palindrome";  
}

This way, PHP won’t lose precision, and palindrome checks remain reliable.

3. Can negative numbers or numbers with leading zeros be palindrome in PHP, and how should code handle them?

Most blogs skip this nuance. By strict definition:

  • Negative numbers like -121 are not palindromes, because the - sign breaks symmetry.
  • Numbers with leading zeros (e.g., 010) should be treated as strings, not numbers, because integers in PHP drop leading zeros.

Example Handling:

$num = "010";  
if ($num === strrev($num)) {  
    echo "$num is Palindrome";  
} else {  
    echo "$num is Not Palindrome";  
}

Output:

010 is Palindrome

So, if input can have leading zeros, always treat it as string input instead of integer.

4. What about palindrome checks for numeric values in different bases (binary, hex) in PHP?

Competitors rarely touch non-decimal palindromes. But in cryptography, compression, and algorithm problems, base representation matters.

Binary Palindrome Example:

$num = 9; // binary: 1001
$binary = decbin($num);

if ($binary === strrev($binary)) {
    echo "$num is Palindrome in Binary";
} else {
    echo "$num is Not Palindrome in Binary";
}

Output:

9 is Palindrome in Binary

Hex Palindrome Example:

$num = 43690; // hex: AAAA
$hex = dechex($num);

if ($hex === strrev($hex)) {
    echo "$num is Palindrome in Hex";
} else {
    echo "$num is Not Palindrome in Hex";
}

Output:

43690 is Palindrome in Hex

This opens up advanced applications in encryption keys, checksum validation, and bit-level operations.

5. How to test or profile which approach (string vs arithmetic vs half-reverse) is faster for real-world large inputs in PHP?

Most answers online just say “string is easier, arithmetic is better for interviews” without proof. But we can benchmark in PHP using microtime(true).

Benchmark Example:

<?php
function checkStrrev($num) {
    return ($num == strrev($num));
}

function checkArithmetic($num) {
    $temp = $num;
    $reverse = 0;
    while ($num > 0) {
        $reverse = $reverse * 10 + $num % 10;
        $num = intdiv($num, 10);
    }
    return $temp == $reverse;
}

$num = 123454321; // Large input

$start = microtime(true);
for ($i=0; $i<100000; $i++) checkStrrev($num);
echo "strrev() Time: " . (microtime(true) - $start) . " sec\n";

$start = microtime(true);
for ($i=0; $i<100000; $i++) checkArithmetic($num);
echo "Arithmetic Time: " . (microtime(true) - $start) . " sec\n";
?>

Findings:

  • For small/medium numbers, strrev() is usually faster because it’s written in C.
  • For very large inputs or when working with half-reverse, arithmetic method may reduce operations and memory usage.
  • In competitive coding, arithmetic is preferred; in production apps, strrev() is practical.

Conclusion

Completing ‘Palindrome Number in PHP’ strengthens problem-solving skills and enhances understanding of PHP string manipulation. It’s truly rewarding to see your code correctly identify palindromes. Why not give it a shot? For more programming insights including Java, Python, and C, visit Newtum today!

Edited and Compiled by

This article was compiled and edited by @rasikadeshpande, who has over 4 years of experience in writing. She’s passionate about helping beginners understand technical topics in a more interactive way.

About The Author