Check whether a number or string is a palindrome in PHP

A palindrome reads the same forwards and backwards. The digits of 12321 form a palindrome. The word “Madam” also qualifies if you ignore letter case. Decide how to handle case, punctuation, and spaces before comparing text.

A simple example

Compare a word with its reverse

  1. Start with aba
  2. Do this Read from the other end
  3. Result aba
Compare the outside letters first. Both are a, and the middle letter stays b.

Check a reversible locker code

  1. Start with Code 1221
  2. Do this Read the digits backwards
  3. Result 1221 matches
A locker-code checker can flag codes that read the same from either end. With code 1231 the reverse is 1321, so it fails.
  1. Integer Palindrome
  2. String Palindrome