Comparing Strings in PHP

PHP provides several functions for comparing strings. In addition to case-sensitive comparison of two strings with strcmp, you can also perform case-insensitive comparisons with strcasecmp, natural order comparisons with strnatcmp and strnatcasecmp, and comparison of a specified number of characters at the beginning of strings with strncmp. You can also compare one string with a selected portion of another string using substr_compare. Plus you can use comparison operators to compare strings. We will start there.

Comparing Strings with Comparison Operators

PHP comparison operators can be applied to strings. You can check whether two values are equal (==) or whether they are identical (===). In order to be considered identical, they must have the same value and the same type, while the equal operator performs type conversion when necessary. Consider the following example:

$val = 27; // integer
$val2 = '27'; // string

// check if equal (==)
if ( $val == $val2 ) {
    echo 'equal';
} else {
    echo 'not equal';
}
// result: equal

// check if identical (===)
if ( $val === $val2 ) {
    echo 'identical';
} else {
    echo 'not identical';
}
// result: not identical

The results of the comparisons tell us that although an integer 27 is equal to a string '27', they are not identical.[1]

Comparing Strings Using Comparison Functions

Pass two string arguments to PHP's comparison functions and they will return 0 if the two strings are equal, an integer less than zero if the first string is less than the second, or an integer greater than zero if the first string is greater than the second.[2]

The most basic of the string comparison functions is strcmp. Let's see what happens when we compare the variables we used in the above example:

// compare integer 27 and string '27'
echo strcmp($val, $val2); // 0

A return value of 0 indicates that the strcmp function has found the two values to be equal. Although the strcmp function has successfully converted a non-string value to a string for this comparison, you will generally want to be sure that the arguments you pass to string functions are strings. Find out more in our discussion on Variable Type.

Case Sensitivity

The strcmp function performs a case-sensitive comparison. We demonstrate by comparing 'January' with 'january':

echo strcmp('January', 'january'); // -32 

The result is an integer less than zero, indicating that the first string is less than the second, or in other words, the two strings are not equal. To perform a case-insensitive comparison, use strcasecmp:

echo strcasecmp('January', 'january'); // 0

This time the result is 0, indicating that according to the strcasecmp function, the two strings are equal.

Natural Order Comparison

Perhaps you have noticed that when comparing alphanumeric strings, the string comparison functions order them in ways that seemed unnatural, even illogical. Consider for example these two strings:

$str1 = 'img4.jpg';
$str2 = 'img20.jpg';

// compare using strcmp
echo strcmp($str1, $str2); // 512

According to the strcmp function, the first string is greater than the second. But how can that make sense, you might wonder? After all, 4 is less than 20. PHP provides the strnatcmp function for this type of comparision:

// natural order comparison of the two strings from the example above
echo strnatcmp($str1, $str2); // -1

The result of this comparison finds the first string is less than the second, which is often more desirable for alphanumeric strings.

PHP provides the strnatcasecmp function for case-insensitive natural order comparisons. Consider the example shown below:

$str3 = 'img_4.jpg';
$str4 = 'Img_20.jpg';
// case-sensitive natural order comparison
echo strnatcmp($str3, $str4); // 1
// case-insensitive natural order comparison
echo strnatcasecmp($str3, $str4); // -1

If you take case into account in the comparison, $str3 is greater than $str4. If you want a case-insensitive natural order comparison of the two strings, you can use the strnatcasecmp function which finds that $str3 is less than $str4.

Comparing the First Part of Strings

PHP provides functions that compare a specified number of characters at the beginning of two strings. The strncmp function performs a case-sensitive comparison while the strncasecmp function performs a case-insensitive comparison. The following demonstrates strncmp:

$str1 = 'jan10';
$str2 = 'jan04';
// compare the first three letters of the two strings above
echo strncmp($str1, $str2, 3); // 0

We pass two strings and an integer that specifies the number of initial characters to compare in the two strings. Our example returns 0 telling us that the initial three characters of the two strings are equal.

The strncasecmp function operates the same way and takes the same arguments. The only difference is that it is case insensitive.

Substring Comparison

The substr_compare function provides the capability of comparing a string with any part of a longer string. An offset argument specifies where in the main string the comparison should begin, and an optional length argument specifies where it should end. The following example demonstrates:

$str = 'brownies_qty';
// get position of underscore in $str using strrpos
$pos = strrpos($str, '_');
/* substr_compare arguments:
 * main string, substring, offset, length (optional) */
// compare $str (from offset 0 to position of underscore) with 'brownies'
echo substr_compare($str, 'brownies', 0, $pos); // 0

The return value of substr_compare is the same as described above for the other string comparison functions. A return value of 0 tells us that our substring ('brownies') is equal to the portion of $str specified by the offset and length arguments.

Find more details about the arguments and options available for substr_compare in the PHP Manual. Find out more about strrpos in our presentation on PHP search functions.


  1. The !== (not identical) operator also takes into account type when performing comparisons, whereas != (not equal), >, <, >=, and <= operators do not. They will perform type conversion when their operands are not the same type. ^
  2. Less than/greater than is determined by the order in the ASCII Table. ^