Como comparar strings em php

In this tutorial, we are going to see how to compare two strings in PHP. You can use strcmp() function in PHP to compare two strings. This function takes two string ‘str1’ and ‘str2’ as parameters. strcmp() function returns < 0 if ‘str1’ is less than ‘str2’; returns > 0 if ‘str1’ is greater than ‘str2’ and 0 if they are equal.
 

<?php $str1 = "Welcom to"; $str2 = "Welcom to StackHowTo"; echo strcmp($str1, $str2); ?>

Output:

-12

 
 
strcmp() function compares two strings in case-sensitive. If you want a case insensitive comparison, you can use the strcasecmp() function, here is an Example.
 

How to compare two strings without case sensitive
<?php $str1 = "welcom to stackhowto"; $str2 = "Welcom to StackHowTo"; echo strcasecmp($str1, $str2); ?>

Output:

0

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; $val2 = '27'; if ( $val == $val2 ) { echo 'equal'; } else { echo 'not equal'; } if ( $val === $val2 ) { echo 'identical'; } else { echo '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:

echo strcmp($val, $val2);

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');

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');

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 seem unnatural, even illogical. Consider for example these two strings:

$str1 = 'img4.jpg'; $str2 = 'img20.jpg'; echo strcmp($str1, $str2);

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:

echo strnatcmp($str1, $str2);

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'; echo strnatcmp($str3, $str4); echo strnatcasecmp($str3, $str4);

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'; echo strncmp($str1, $str2, 3);

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.

The substr_compare function is used to compare 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'; $pos = strrpos($str, '_'); echo substr_compare($str, 'brownies', 0, $pos);

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 and in our discussion of the related substr function. Find out more about strrpos in our presentation on PHP search functions.

Back to top