Compare Strings In PHP

Almost every programming supports the facility of comparing values ​​for different variables. When building applications in PHP, you may encounter a situation where you may need to compare values. If the values ​​you are comparing are Boolean or integer values, the comparison is straightforward; however, if you are planning to compare a string or a part of a complete string, then the comparison criteria increases. Because in that case, you have to compare whether the string is in lower case or upper case. In this tutorial, you will learn about different ways to compare strings in PHP.

strcmp() Function

The first way of comparing strings in PHP is by using the strcmp() function of PHP for easily comparing two strings. The function has all the logic written within it. This function will take two strings as parameter 'strg1' and 'strg2'. Then this function will return < 0 if the first string (i.e. strg1) is less than the second string(i.e. strg2); will return > 0 if the first string (i.e. strg1) is greater than a second-string (i.e. strg2), and 0 if both the strings are equal.

Syntax:
strcmp(string1,string2)

Let us find this using an example for understanding how the function will works:

Example:
<?php

$strg1 = "Hey";
$strg2 = "Hey PHP";

echo strcmp($strg1, $strg2); // Outputs: 4

?>

== operator

Another simplified approach of comparing strings is comparing two strings with the help of one common operator usually used for comparing values within a conditional statement. It is the == operator, which will check whether two strings are equal or not. If yes, then it will return true.

Here is a simple code snippet of using the equal operator for string manipulation:

Example:
<?PHP

$strg1 = "Hey";

$strg2 = "Hey PHP";

if($strg1 == $strg2)
{
     echo 'The strings are matching.';
} else {
     echo 'The strings are not matching.';
}

?>

The above condition will return a FALSE because the first string (strg1) is not equal to the second string (strg2). Again, if the letters in the first string were in uppercase but the same and the letters in the second string, but in lower case, then this comparison with the operator would have return FALSE.

Here is a code snippet explaining this:

Example:
<?PHP

$strg1 = "hey";
$strg2 = "HEY";

if(strg1 == strg2)
{
     echo 'The strings are matching';
} else {
     echo 'The strings are not matching';
}

?>

Both are different because the comparison makes use of the ASCII value, and since lowercase a (whose ASCII value is 97) and uppercase A (whose ASCII value is 65) are distinct.

strcasecmp() function

strcasecmp is another predefined function used for comparing two given strings. It is case insensitive as well as binary safe (means, it does not return 0s or 1s). It will return zero (0) if both the strings are equal.

Again, when the first string(strg1) is lesser than the second string (strg2), returns < 0, and when the first string (strg1) is greater than the second string (strg2), then it returns> 0.

Example:
<?php

$strg1 = "world";
$strg2 = "WORLD";

echo "1st string:".$strg1;
echo "2nd string:".$strg2;

echo strcasecmp($strg1,$strg2);

?>
Output:
1st string: world

2nd string: WORLD

0

strncasecmp() Function

strncasecmp is another predefined function used for comparing two given strings. It is case insensitive as well as binary-safe. The string comparison is of the first n characters. The only difference between strcasecmp() and strncasecmp() is that you can postulate the number of characters from each string to be implemented in your comparison. ). It will return zero (0) if both the strings are equal.

Again, when the first string(strg1) is lesser than the second string (strg2), returns < 0, and when the first string (strg1) is greater than the second string (strg2), then it returns> 0.

Syntax:
strncasecmp(string1,string2,length)
Example:
<?php

$strg1 = "helloworld";
$strg2 = "hello";

echo strncasecmp($strg1, $strg2 , 5);

?>

If you run the above code, it will return 0, because the checking for comparison is being done only on the first five characters, as given in the third parameter (5).


Here are few other related articles for you to read:

Scroll Back to Top