Open In App

How to Compare Two Strings without using strcmp() function in PHP ?

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Comparing two strings is a common operation in programming. In PHP, the strcmp() function is typically used to compare two strings. However, there may be situations where you need to compare strings without using this built-in function, such as for learning purposes or to meet specific constraints. In this article, we will explore different approaches to comparing two strings in PHP without using the strcmp() function.

Compare Two Strings using a Loop

One way to compare two strings is by iterating through each character and comparing them individually.

PHP




<?php
  
function compareStrings($str1, $str2) {
    if (strlen($str1) != strlen($str2)) {
        return false;
    }
  
    for ($i = 0; $i < strlen($str1); $i++) {
        if ($str1[$i] != $str2[$i]) {
            return false;
        }
    }
    return true;
}
  
// Driver code
$str1 = "hello";
$str2 = "hello";
  
if (compareStrings($str1, $str2)) {
    echo "The strings are equal.";
} else {
    echo "The strings are not equal.";
}
  
?>


Output

The strings are equal.


Explanation:

  • The compareStrings function first checks if the lengths of the two strings are equal. If not, the strings are not equal.
  • It then iterates through each character of the strings and compares them. If any characters are different, the function returns false.
  • If all characters are the same, the function returns true.

Compare Two Strings using Array Functions

Another approach is to convert the strings to arrays and use array functions to compare them.

PHP




<?php
  
function compareStrings($str1, $str2) {
    return count(array_diff_assoc(
        str_split($str1), str_split($str2))) === 0;
}
  
// Driver code
$str1 = "Geeks";
$str2 = "geeks";
  
if (compareStrings($str1, $str2)) {
    echo "The strings are equal.";
} else {
    echo "The strings are not equal.";
}
  
?>


Output

The strings are not equal.


Explanation:

  • The compareStrings function uses the str_split() function to convert the strings to arrays of characters.
  • It then uses the array_diff_assoc() function to compare the arrays. If there are any differences, the resulting array will have elements, and its count will be non-zero.
  • If the count is zero, the arrays (and thus the strings) are equal.

Compare Two Strings using a Hashing Function

For a more advanced approach, you could use a hashing function to compare the strings.

PHP




<?php
  
function compareStrings($str1, $str2) {
    return hash('sha256', $str1) === hash('sha256', $str2);
}
  
// Driver code
$str1 = "Geeks";
$str2 = "Geeks";
  
if (compareStrings($str1, $str2)) {
    echo "The strings are equal.";
} else {
    echo "The strings are not equal.";
}
  
?>


Output

The strings are equal.


Explanation:

  • The compareStrings function uses the hash function to generate a SHA-256 hash of each string.
  • It then compares the hashes. If the hashes are equal, the strings are considered equal.

Compare Two Strings ising implode() and aray_map() Functions

In this section, compare two strings using the implode() and array_map() functions in PHP. Steps to compare two strings:

  • Convert each string into an array of characters using str_split() function.
  • Use array_map() function to compare each corresponding character in the two arrays.
  • Use implode() function to concatenate the results of the comparison into a single string.
  • Check if the resulting string contains only “1” (which indicates that all characters are equal) to determine if the two strings are the same.

PHP




<?php
  
function compareStr($str1, $str2) {
    $arr1 = str_split($str1);
    $arr2 = str_split($str2);
  
    $result = array_map(function($char1, $char2) {
        return $char1 === $char2 ? '1' : '0';
    }, $arr1, $arr2);
  
    $comparisonResult = implode('', $result);
  
    return strpos($comparisonResult, '0') === false;
}
  
// Driver code
$str1 = "Geeks";
$str2 = "Geeks";
$str3 = "Hello";
  
echo (compareStr($str1, $str2) ? "Equal" : "Not Equal") . "\n";
echo compareStr($str1, $str3) ? "Equal" : "Not Equal";
  
?>


Output

Equal
Not Equal

In this program, the compareStr() function takes two strings as input and returns true if they are equal and false otherwise.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads