Skip to content
Advertisement

Using PHP write an anagram function?

Using PHP write an anagram function? It should be handling different phrases and return boolean result.

Usage:

$pharse1 = 'ball';
$pharse2 = 'lbal';
if(is_anagram($pharse1,$pharse2)){
  echo $pharse1 .' & '. $pharse2 . ' are anagram';
}else{
  echo $pharse1 .' & '. $pharse2 . ' not anagram';
}

Advertisement

Answer

There’s simpler way

function is_anagram($a, $b) {
    return(count_chars($a, 1) == count_chars($b, 1));
}

example:

$a = 'argentino';
$b = 'ignorante';
echo is_anagram($a,$b);   // output: 1

$a = 'batman';
$b = 'barman';
echo is_anagram($a,$b);  // output (empty):
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement