PHP string based comparison function strcmp of and strcasecmp of use details

  • 2020-06-03 05:55:31
  • OfStack

The difference between "===" and "==" is simply that the former emphasizes that "identical(same, exactly the same)" requires one; The latter requires "equal" and the same value is sufficient. Or use strcmp, but this will tell you if two strings are equal, but not where they are different.

1 as good as possible! Equals, equals, compares two objects to see if they're equal, two objects, because they're not all strings, they're integers, and so on. Such as


$a = "joe"; 
$b = "jerry"; 
if ($a != $b) 
{ 
    echo " Not equal to the "; 
} 
else 
{ 
    echo " equal "; 
}

If use! To return true, the types of the two objects must be strictly equal. Or use the = =,! = automatically converts the string to the appropriate type for comparison.

22 == "22"; //  return  true 
22 === "22"; //  return falsePHP  Functions for string comparison: strcmp(),strcasecmp(),strncasecmp(), strncmp() They all are   If the former is larger than the latter , Returns greater than 0  The integer; If the former is less than the latter, the return is less than 0  The integer; If the two are equal, returns 0.

1)strcmp Is used for   Case sensitivity  ( Case sensitive ) String comparison of: 
2)echo strcmp("abcdd", "abcde"); //  return  1 (>0),  The comparison is  "b" and "b"
3)strcasecmp For case-insensitive string comparisons: 
4)echo strcasecmp("abcdd", "abcde"); //  return  -1 (<0),  The comparison is "d" and "e"
strncmp Used to compare strings 1 Section, starting at the beginning of the string, at 3 Is the length to be compared: 
echo strncmp("abcdd", "abcde", 3); //  return  1 (>0),  Comparison of the  abc  and  abc
strncasecmp Used for case-insensitive comparison strings 1 Section, starting at the beginning of the string, at 3 Is the length to be compared: 
echo strncasecmp("abcdd", "abcde", 3); //  return  0,  Comparison of the  abc  and  abc,  They are the same because they are case insensitive. 
 more 1 In this case, the comparison of string sizes alone does not meet our pre-determined requirements, such as business as usual  10.gif  than  5.gif  Large, but if you apply the above functions, it returns  -1, It indicates that the  10.gif than 5.gif, In this case, php Two natural contrast functions are provided strnatcmp,strnatcasecmp : 
echo strnatcmp("10.gif", "5.gif"); //  return  1 (>0) 
echo strnatcasecmp("10.gif", "5.gif"); //  return  1 (>0)


Related articles: