2

How to Make String Palindrome in PHP

How to Make String Palindrome: A string is said to be Palindrome, if its reverse string is the same as it is!.

Let suppose we have a string = “TAT ” now if we reverse it !

it will still be TAT. so we simply say that the string is palindrome.Lets look at the simple example to understand all this.


<?php

$str = "tat";
$reverse_str= strrev($str);
if( $reverse_str == $str){

echo"the string ".$str."is Palindrome";
}
else
{
echo"the string ".$str."is not Palindrome";
}
?>

 

Now lets try the same Palindrome without using strrev() function.

Palindrome without using strrev() function in php

<?php

$str = “level”;
$str_array = str_split($str);
$str_reverse =””;
$size = count($str_array);
echo”the size of string is “.$size;

for($i=$size-1; $i>=0;$i–){

$str_reverse.=$str_array[$i];

}
if($str_reverse == $str){

echo “this is Palindrome”;}

else{
echo “this is not Palindrome”;
}

?>

Please do post your comments .

 

admin
 

student of computer Science , having interest and experience in C++,Java,Php,Html,Css,JavaScript, wordpress and android development

  • dev

    giving string in above programme output shows not pallindrom how..but level is a pallindrom..

  • Miru Saeedian

    Excellent work Sir!!! That’s what I think to be published for the beginners.