0
0
PhpProgramBeginner · 2 min read

PHP Program to Check Palindrome Number

You can check a palindrome number in PHP by converting the number to a string, reversing it with strrev(), and comparing it to the original string; for example, $isPalindrome = ($num == strrev((string)$num));.
📋

Examples

Input121
Output121 is a palindrome number.
Input123
Output123 is not a palindrome number.
Input0
Output0 is a palindrome number.
🧠

How to Think About It

To check if a number is a palindrome, think of it like reading a word forwards and backwards. Convert the number to a string so you can reverse it easily. Then compare the reversed string to the original. If they match, the number is a palindrome.
📐

Algorithm

1
Get the input number.
2
Convert the number to a string.
3
Reverse the string.
4
Compare the reversed string with the original string.
5
If they are the same, return that the number is a palindrome.
6
Otherwise, return that it is not a palindrome.
💻

Code

php
<?php
$num = 121;
$strNum = (string)$num;
$revStrNum = strrev($strNum);
if ($strNum === $revStrNum) {
    echo "$num is a palindrome number.";
} else {
    echo "$num is not a palindrome number.";
}
?>
Output
121 is a palindrome number.
🔍

Dry Run

Let's trace the number 121 through the code to see how it checks for palindrome.

1

Input number

num = 121

2

Convert to string

strNum = "121"

3

Reverse string

revStrNum = "121"

4

Compare strings

"121" === "121" is true

5

Output result

Print '121 is a palindrome number.'

StepstrNumrevStrNumComparison Result
1121121true
💡

Why This Works

Step 1: Convert number to string

We convert the number to a string so we can easily reverse it using string functions.

Step 2: Reverse the string

Using strrev(), we reverse the string to check if it reads the same backwards.

Step 3: Compare original and reversed

If the reversed string matches the original, the number is a palindrome.

🔄

Alternative Approaches

Using arithmetic to reverse number
php
<?php
$num = 121;
$original = $num;
$reverse = 0;
while ($num > 0) {
    $digit = $num % 10;
    $reverse = $reverse * 10 + $digit;
    $num = (int)($num / 10);
}
if ($original === $reverse) {
    echo "$original is a palindrome number.";
} else {
    echo "$original is not a palindrome number.";
}
?>
This method uses math to reverse the number without converting to string, which can be faster for large numbers.
Using recursion to check palindrome
php
<?php
function isPalindromeStr($str) {
    if (strlen($str) <= 1) return true;
    if ($str[0] !== $str[strlen($str) - 1]) return false;
    return isPalindromeStr(substr($str, 1, -1));
}
$num = 121;
if (isPalindromeStr((string)$num)) {
    echo "$num is a palindrome number.";
} else {
    echo "$num is not a palindrome number.";
}
?>
This recursive method checks characters from outside in, but uses more memory and calls.

Complexity: O(n) time, O(n) space

Time Complexity

The time depends on the length of the number's string form, as reversing and comparing take linear time.

Space Complexity

Extra space is used to store the string and its reversed copy, so space is linear in the number of digits.

Which Approach is Fastest?

The arithmetic method avoids string conversion and uses constant space, making it faster and more memory efficient for large numbers.

ApproachTimeSpaceBest For
String reverseO(n)O(n)Simple and readable code
Arithmetic reverseO(n)O(1)Performance with large numbers
Recursive string checkO(n)O(n)Learning recursion, less efficient
💡
Always convert the number to a string first to simplify palindrome checking in PHP.
⚠️
Beginners often forget to convert the number to a string before reversing, causing errors.