PHPRO.ORG

PHP Check If String Is A Palindrome

PHP Check If String Is A Palindrome

This function, as the name suggests, will check to see if a given string is a palindrome. That is, it reads the same backwards, as it does forwards.

Impress your friends, scare you pets! You will be A Santa at NASA with this function in your tool kit.


<?php 

/**
 *
 * Check if a string is a palindrome
 *
 * @param    string    $string
 * @return    bool
 *
 */
function checkPalindrome$string )   
{
    
// strip out whitespace
    
$string str_replace' '''$string );
    
// return bool
    
return $string == strrev$string );
}  

// a string to check
$string 'sex at noon taxes';

// call the function and check the return value
if( checkPalindrome$string ) == true )
{
    
// if function returns true
    
echo 'String is a palindrome';
}
else
{
    
// if function returns false
    
echo 'string is not a palindrome';
}

?>