PHP Program to Check Palindrome String
Use
strrev() to reverse the string and compare it with the original using if ($string === strrev($string)) to check if it is a palindrome.Examples
Inputmadam
Outputmadam is a palindrome
Inputhello
Outputhello is not a palindrome
InputA
OutputA is a palindrome
How to Think About It
To check if a string is a palindrome, think about reading it forwards and backwards. If both are exactly the same, then the string is a palindrome. So, reverse the string and compare it with the original string to decide.
Algorithm
1
Get the input string.2
Reverse the input string.3
Compare the original string with the reversed string.4
If both are equal, return that it is a palindrome.5
Otherwise, return that it is not a palindrome.Code
php
<?php $string = "madam"; if ($string === strrev($string)) { echo "$string is a palindrome"; } else { echo "$string is not a palindrome"; } ?>
Output
madam is a palindrome
Dry Run
Let's trace the string 'madam' through the code
1
Input string
$string = "madam"
2
Reverse string
strrev($string) = "madam"
3
Compare strings
"madam" === "madam" is true
4
Output result
Print 'madam is a palindrome'
| Original String | Reversed String | Comparison Result |
|---|---|---|
| madam | madam | true |
Why This Works
Step 1: Reverse the string
The strrev() function creates a reversed copy of the input string.
Step 2: Compare original and reversed
Using === checks if both strings are exactly the same, including case.
Step 3: Print result
If they match, the string is a palindrome; otherwise, it is not.
Alternative Approaches
Manual character comparison
php
<?php $string = "madam"; $len = strlen($string); $isPalindrome = true; for ($i = 0; $i < $len / 2; $i++) { if ($string[$i] !== $string[$len - $i - 1]) { $isPalindrome = false; break; } } if ($isPalindrome) { echo "$string is a palindrome"; } else { echo "$string is not a palindrome"; } ?>
This method checks characters one by one without creating a reversed string, saving memory for very long strings.
Using strtolower for case insensitive check
php
<?php $string = "Madam"; $lower = strtolower($string); if ($lower === strrev($lower)) { echo "$string is a palindrome"; } else { echo "$string is not a palindrome"; } ?>
This approach ignores case differences by converting the string to lowercase before checking.
Complexity: O(n) time, O(n) space
Time Complexity
The program reverses the string which takes O(n) time where n is the string length, and then compares two strings in O(n) time, so overall O(n).
Space Complexity
The reversed string requires O(n) extra space. The manual comparison alternative uses O(1) space.
Which Approach is Fastest?
Using strrev() is simple and fast for most cases, but manual character comparison saves memory for very large strings.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Using strrev() | O(n) | O(n) | Simple and quick palindrome check |
| Manual character comparison | O(n) | O(1) | Memory efficient for large strings |
| Case insensitive check with strtolower | O(n) | O(n) | Ignoring letter case in palindrome check |
Use
strrev() for a quick palindrome check in PHP.Forgetting to use strict comparison
=== can cause incorrect results if types differ.