How to Compare Two Strings without using strcmp() function in PHP ?
Last Updated :
27 Feb, 2024
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.";
}
?>
OutputThe 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.";
}
?>
OutputThe 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.";
}
?>
OutputThe 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";
?>
In this program, the compareStr() function takes two strings as input and returns true if they are equal and false otherwise.
Similar Reads
How to check if a String Contains a Substring in PHP ? Checking whether a string contains a specific substring is a common task in PHP. Whether you're parsing user input, filtering content, or building search functionality, substring checking plays a crucial role.MethodsBelow are the following methods by which we can check if a string contains a substri
1 min read
How to get a substring between two strings in PHP? To get a substring between two strings there are few popular ways to do so. Below the procedures are explained with the example.Examples: Input:$string="hey, How are you?"If we need to extract the substring between "How" and "you" then the output should be are Output:"Are"Input:Hey, Welcome to Geeks
4 min read
How to check a string starts/ends with a specific string in PHP ? In this article, we are going to learn how to check that a given string is starting or ending with a given specific string in PHP.We require the use of XAMPP, Netbeans to run this code.Input: "Geeks For Geeks"Starting with: "G"Output: "Geeks For Geeks Starts with String G"In PHP version 8+, there ar
2 min read
Concatenation of two strings in PHP In this article, we will concatenate two strings in PHP. There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right
2 min read
PHP substr_compare() Function The substr_compare() function is a built-in function in PHP and it helps to compare two strings from a specified start position upto a specified length. Syntax: int substr_compare($str1, $str2, $startpos, $len, $caseInsensitive) Parameters: This function accepts a total of five parameters out of whi
2 min read