PHP String Compare With Examples

The PHP String Compare function is used for comparing two strings.

In this article, we will talk about PHP String Compare in a detailed explanation as well as example programs that could be helpful to your future development. This is a continuation of the previous topic, PHP modulo Operator.

What is PHP String Compare?

In PHP, string compare is the process of evaluating two strings in order to determine if they were equal or not. The process can simply be done with the use of various comparison functions such as strcmp(), strcasecmp(), strnatcmp(), and soon.

Further, the result of this function could be a numerical value that represents the difference between the two strings. The 0 value will indicate the string is equal, then the value less than 0 will indicate the first string that is less than the second, and lastly, the value that is greater than 0 will indicate that the string1 is greater than string2.

Syntax

strcmp(string $stringOne, string $stringTwo): int

Example

<?php
    $first = "ITSOURCECODE";
    $second = "itsourcecode";
        if (strcmp($first, $second) !== 0) {
            echo '$first is not equal to $second in a case sensitive string comparison';
    }
?>

Output

$first is not equal to $second in a case sensitive string comparison

How to compare two strings in PHP?

The following are several ways to compare two strings in PHP:

  • strcmp() function
  • strcasecmp() function
  • strnatcmp() function

PHP strcmp() function

The strcmp() function is used to compare two strings in a dictionary order that will return an integer value and will be indicating a result of the binary safe string comparison function. Once the two strings are equal it will return 0, then a negative value once the first string is less than the second string, and the positive value once the first string is greater than the second string.

Example:

<?php
	$stringOne = "ITSOURCECODE";
  $stringTwo = "ITSOURCECODE";
    $result = strcmp($stringOne, $stringTwo);
    if ($result == 0) {
       echo "The strings are equal.";
    } elseif ($result < 0) {
       echo "The first string is less than the second.";
    } else {
      echo "The first string is greater than the second.";
    }
?>

Output:

The strings are equal.

PHP strcasecmp() function

The strcasecmp() function is used to compare two strings in a dictionary order that ignores the case of characters Once the two strings are equal it will return 0, Then a negative value once the first string is less than the second string, and a positive value once the first string is greater than the second string.

Example:

<?php
	$stringOne = "ITSOURCECODE";
  $stringTwo = "itsourcecod";
    $result = strcasecmp($stringOne, $stringTwo);
      if ($result == 0) {
          echo "The strings are equal.";
      }elseif($result < 0) {
          echo "The first string is less than the second.";
      }else{
          echo "The first string is greater than the second.";
      }
?>

Output:

The first string is greater than the second.

PHP strnatcmp() function

The strnatcmp() is a function that compares two strings with the use of (natural order), which is similar to the way humans sort directories and files, Once the two strings, are equal it will return 0, then a negative value once the first string is less than the second string and a positive value once the string is greater than the second string.

Example:

<?php
$stringOne = "itsourcecodeOne";
$stringTwo = "itsourcecodeTwo";
$result = strnatcmp($stringOne, $stringTwo);
    if($result == 0) {
       echo "The strings are equal.";
    }elseif ($result < 0){
       echo "The first string is less than the second.";
    }else{
       echo "The first string is greater than the second.";
    }
?>

Output:

The first string is less than the second.

Summary

In summary, you have learned about PHP String Compare. This article also discussed how to compare two strings in PHP in three different ways such as PHP strcmp(), strcasecmp(), and, strnatcmp().

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.

Leave a Comment