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


April 14, 2022, Learn eTutorial
1400

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 Anagram. Other examples of Anagram are 'peek' and 'keep'. Here we can see same alphabets are used in a different order to form meaningful words. 

anagram strings?

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

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

For making the comparison easy, we change both strings to lowercase using strtolower() function in PHP. Then use str_split() function to convert that 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 those 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 those 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 they are anagram otherwise print that they are not an anagram


For solving the anagram string program in PHP we need to learn about the below topics, please refer to those for a better understanding

PHP Source Code

                                          $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