PHP Program to check whether the two strings are anagram or not

It is a String based program in PHP. So before moving to the program we should have knowledge of the following topics

What are anagram strings?

Anagram strings are two strings built up by the same set of characters, where the order of characters is the only difference in the strings.

For example, the word 'silent' and 'listen' are anagrams, or 'peek' and 'keep' are anagrams. Here we can see same alphabets are used in different order to form meaningful words. 

What are anagram strings?

How to check whether the two strings are anagrams or not using PHP?

In this program, we are accepting the strings from the user and checking whether the strings are anagram or not.

For making string comparison easy we change both strings to lowercase using strtolower() function in PHP. Then use str_split() function to convert the strings into an array. Next, we will sort these arrays using sort() and convert the arrays again into a string using implode() function. Now we got strings in the same order of characters so use strcmp() function to compare these strings and if both are the same then they are anagrams otherwise they are not anagram strings.

ALGORITHM

Step 1: Accept the strings into the variables str1 and str2

Step 2: Assign these strings in the variables str1 and str2 into the variables s1 and s2

Step 3: Check if both strings are of the same length using the condition strlen(s1) != strlen(s2), if true then print that the entered strings are not anagram and exit the program, otherwise perform the following steps

Step 4: Convert the strings in the variables s1 and s2 into lowercase by using the built-in function strtolower() and assign it to the variables s1 and s2

Step 5: Convert the strings in the variables s1 and s2 into array by using the built-in function str_split() and assign it to the variables let1 and let2

Step 6: Sort the array let1 and let2 by using the built-in function sort()

Step 7: Convert the array let1 and let2 into the strings by using the built-in function implode()

Step 8: Check the condition strcmp(s1, s2) == 0, if true print that the entered strings are anagram otherwise print that the entered strings are not an anagram

PHP Source Code

                                          <?php
$str1 = readline("Enter the 1st string: ");
$str2 = readline("Enter the 2nd string: ");
$s1 = $str1;
$s2 = $str2;
if (strlen($s1) != strlen($s2)) {
    echo "The entered strings $str1 and $str2 are not anagram";
} else {
    $s1 = strtolower($s1);
    $s2 = strtolower($s2);
    $let1 = str_split($s1);
    $let2 = str_split($s2);
    sort($let1);
    sort($let2);
    $s1 = implode("", $let1);
    $s2 = implode("", $let2);
    if (strcmp($s1, $s2) == 0) {
        echo "The entered strings $str1 and $str2 are anagram";
    } else {
        echo "The entered strings $str1 and $str2 are not anagram";
    }
}
?>
                                      

OUTPUT

Example 1
Enter the 1st string: race
Enter the 2nd string: care
The entered strings race and care are anagram

Example 2
Enter the 1st string: earth
Enter the 2nd string: heart
The entered strings earth and heart are anagram