PHP Palindrome: Check Palindrome Number & String

Introduction

A palindrome is a simple yet powerful concept used to test logical thinking, loops, and string handling skills. In PHP, checking whether a string or number is a palindrome is a frequently asked question for beginners.

In this article, you will learn what a palindrome is and how to implement palindrome logic in PHP using different approaches.

What is a Palindrome?

A palindrome is a word, number, or sequence that reads the same forward and backward.

OR

If reversing a value gives the same result, it is a palindrome.

Examples:

madam → palindrome 

racecar → palindrome 

121 → palindrome 

hello → not palindrome

📖
Important Points:
  • Can be a string or a number
  • Case sensitivity may matter
  • Spaces and special characters can affect results

Syntax (General Logic)

To check palindrome:

  1. Take a string or number
  2. Reverse it
  3. Compare with original
  4. If equal → palindrome

Examples:

Example 1: Palindrome String (Using Loop)


<?php
$string = "madam";
$reverse = "";
for ($i = strlen($string) - 1; $i >= 0; $i--) {
   $reverse .= $string[$i];
}
if ($string == $reverse) {
   echo "Palindrome";
} else {
   echo "Not Palindrome";
}
?>

Example 2: Using Built-in Function


<?php
$string = "madam";
if ($string == strrev($string)) {
   echo "Palindrome";
} else {
   echo "Not Palindrome";
}
?>

Example 3: Palindrome Number


<?php
$num = 121;
$temp = $num;
$reverse = 0;
while ($temp > 0) {
   $digit = $temp % 10;
   $reverse = ($reverse * 10) + $digit;
   $temp = (int)($temp / 10);
}
if ($num == $reverse) {
   echo "Palindrome Number";
} else {
   echo "Not Palindrome Number";
}
?>

Example 4: Case-Insensitive Palindrome


<?php
$string = "Madam";
$string = strtolower($string);
if ($string == strrev($string)) {
   echo "Palindrome";
}
?>

Example 5: Ignore Spaces


<?php
$string = "nurses run";
$string = str_replace(" ", "", strtolower($string));
if ($string == strrev($string)) {
   echo "Palindrome";
}
?>

Real-Life Example

Scenario 1 : Username Validation


<?php
$username = "level";
if ($username == strrev($username)) {
   echo "Special username (Palindrome)";
}
?>

Scenario 2: Data Checking System


<?php
$data = ["madam", "hello", "racecar"];
foreach ($data as $word) {
   if ($word == strrev($word)) {
       echo "$word is palindrome<br/>";
   }
}
?>

👉 Useful in filtering symmetric strings.

Common Mistakes

1. Ignoring Case Sensitivity


"Madam" != "madaM"

👉 Use strtolower().

2. Not Removing Spaces


"nurses run"

👉 Remove spaces before checking.

3. Incorrect Reverse Logic

Wrong loop logic may produce incorrect results.

4. Using Wrong Comparison Operator

Always use == or === properly.

5. Not Handling Numbers Properly

Numeric palindrome requires different logic.

Conclusion

The palindrome program in PHP is a fundamental concept that helps you understand string manipulation, loops, and logic building. It is widely used in interviews and real-world applications.

By mastering palindrome logic, you can:

  • Improve coding skills
  • Understand string operations deeply
  • Solve complex problems easily