Word Palindrome Check in PHP & Python


Read {count} times since 2020

Palindromes are unique words that are read the same forward and backward. We can identify them using out eyes and brain. How about we identify using a simple program in PHPPython ?

PHP

<?php
$word = strtolower("Malayalam");
$splitted = str_split($word);
$reversedWord = "";
$length = strlen($word);
for($i = 0; $i < $length; $i++)
	$reversedWord .= $splitted[$length - $i - 1];
echo $word == $reversedWord ? "It's a palindrome !n" : "It's not a palindromen";
?>

Instead of the loop to reverse the word, you can also use :

$reversedWord = strrev($word)

instead of :

$splitted = str_split($word);
$reversedWord = "";
$length = strlen($word);
for($i = 0; $i < $length; $i++)
         $reversedWord .= $splitted[$length - $i - 1];

A line break is added to the output to make output neat if the program is ran via terminal.

Python

word = "Malayalam"
word = word.lower()
reversedWord = "";
for i in range(1, len(word) + 1, 1):
	reversedWord += word[-i]
if word == reversedWord :
	print "It's a palindrome !"
else:
	print "It's not a palindrome"

The input word should be given to the variable “word” in both the programs and both will output “It’s a palindrome” if the given word is a palindrome and if not, outputs “It’s not a palindrome”.