Comparing Strings

PHP offers four functions for comparing strings: strcmp(), strcasecmp(), strncmp(), and strcasencmp(). These functions return a positive value when the string passed as the first parameter is greater than the second parameter, and a negative value when it is smaller. If both strings are equal then 0 is returned.

The strcmp() performs a case-sensitive comparison. If no case sensitivity is required, strcasecmp() comes into play. It works as strcmp(); however, it does not distinguish between uppercase and lowercase letters. The functions strncmp() and strcasencmp() can be used to only compare the first “n” characters of two strings.

  1. strcmp()
  2. strcasecmp()
  3. strncmp()
  4. strncasecmp()
  5. How comparison actually made
  6. strnatcmp
  7. strnatcasecmp

Comparing string using == or ===

Comparing strings seems like an easy task using the == operator for implicit type conversion (so '1' == 1 returns true) or the === operator for type checking (so '1' === 1 returns false). However, the first method is rather flawed because the type-conversions are not always turned into strings. For instance, 1 == '1twothree' returns true, because both values are converted into integers. Therefore, === is the way to go.


 strcmp(string $string1, string $string2): int

This function use for binary safe case sensitive string comparison and accepts two parameters:

  1. $string1 first string and
  2. $string2 second string

This function returns:

  • 0 if both $string1 and $string2 are equal
  • < 0 if $string1 is less than $string2
  • > 0 if $string1 is greater than $string2
 $string1 = 'aardvark';
 $string2 = 'zebra';
 echo strcmp($string1, $string2); // -1
 echo strcmp($string1, "Zebra");  //  1
 echo strcmp('mouse', 'mouse');   //  0

 $compare = strcmp($string1, $string2);
 if ($compare > 0 )
  echo "$string1 > $string2";
 elseif ($compare < 0)
  echo "$string1 < $string2";
 elseif ($compare === 0)
  echo "$string1 = $string2";
//Prints: aardvark < zebra


 strcasecmp(string $string1, string $string2): int

This function is identical to strcmp with one differnce, it is case insensitive.

Example: Comparing Strings

  $a = 'PHP';
  $b = 'php';
  echo 'strcmp(): ' . strcmp($a, $b) . '<br />';
  echo 'strcasecmp(): ' . strcasecmp($a, $b);

Which outputs:

strcmp(): -1
strcasecmp(): 0


 strncmp(string $string1, string $string2, int $length): int

This function takes a third argument $length that restricts the comparison to length characters. These examples show the results of various comparisons:

 echo strncmp("aardvark", "aardwolf", 4).'<br>'; //  0
 echo strncmp("aardvark", "aardwolf", 5).'<br>'; // -1


 strncasecmp(string $string1, string $string2, int $length): int

The function strncasecmp() is case-insensitive version of strncmp() function.

 echo strncmp("Aardvark", "aardwolf", 4);    //  -1
 echo strncasecmp("Aardvark", "aardwolf", 4);//  0

The functions strcmp(), strncmp(), strcasecmp(), or strncasecmp() can be used as the callback function when sorting arrays with usort(). More about custom array sorting can be found in “Sorting with a user-defined function“.

Checking Usernames and Passwords

When validating a username and a password (for example, a login form), two things seem to form a de facto standard on the Web:

  • The password is always case-sensitive. It has to be provided exactly the same way it was set.
  • The username, however, is not case-sensitive.

Therefore, a username has to be compared without considering case sensitivity. This can be done either by using strcasecmp() or by first converting the provided username and the real username into lowercase letters (or uppercase letters). This is done by the functions strtolower() or strtoupper(). The preceding code shows an example, using strcmp()/strcasecmp() and also the compare operator ===.

Validating Logins by Comparing Strings

  $user = $_POST['user']) ?? '';
  $pass = $_POST['pass']) ?? '';
  if (
    (strtolower($user) === 'user1' && $pass === 'secret') ||
    (strtoupper($user) === 'USER2' && $pass === 'verysecret') ||
    (strcasecmp($user, 'User3') == 0 && strcmp($pass, 'topsecret') == 0)
  ) {
    echo 'Login successful.';
  } else {
    echo 'Login failed.';

Manipulating Strings:

  1. Changing string case
  2. Comparing Strings
  3. Comparing Strings with Natural Order Algorithm
  4. Converting Strings into HTML
  5. Converting HTML Entities and Special Characters
  6. Formatting Strings
  7. Padding Strings
  8. Replacing Newlines with BR Tag and Trimming Whitespaces
  9. Converting character to code and code to character
  10. Repeat and Shuffle a String
  11. Count Characters and Words
  12. Manipulating substrings