w3resource logo


php exercises

PHP Challenges: Check if a given string is an anagram of another given string

PHP Challenges - 1: Exercise-21 with Solution

Write a PHP program to check if a given string is an anagram of another given string.

Input : ('anagram','nagaram')
Output : These two strings are anagram

According to Wikipedia an anagram is direct word switch or word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once; for example, the word anagram can be rearranged into nag-a-ram.

Explanation :

PHP: Check if a given string is an anagram of another given string

Sample Solution :-

PHP Code :

<?php
function is_anagram($a, $b)
 {
       if (count_chars($a, 1) == count_chars($b, 1))
    {
        return "This two strings are anagram";
    }
    else
    {
        return "This two strings are not anagram";
    }
 }
print_r(is_anagram('anagram','nagaram')."\n");
print_r(is_anagram('cat','rat')."\n");
?>

Output :

This two strings are anagram                                
This two strings are not anagram  

Flowchart:

PHP Flowchart: Check if a given string is an anagram of another given string

PHP Code Editor:

<?php
function is_anagram($a, $b)
 {
       if (count_chars($a, 1) == count_chars($b, 1))
    {
        return "This two strings are anagram";
    }
    else
    {
        return "This two strings are not anagram";
    }
 }
print_r(is_anagram('anagram','nagaram')."\n");
print_r(is_anagram('cat','rat')."\n");
?>

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number