PHP string comparison functions mainly include strcmp, strcasecmp, strnatcmp, strnatcasecmp, and their usage is basically similar. (Recommended learning: PHP programming from entry to proficiency)
//按字节对字符串进行比较 int strcmp(string str1,string str2) //同上,但是不区分大小写 int strcasecmp(string str1,string str2) //按“自然排序”进行比较 int strnatcmp(string str1,string str2) //同上,但是不区分大小写 int strnatcasecmp(string str1,string str2)
The return results of these four functions are the same
If str1 Equal to str2, return 0
If str1 is greater than str2, return 1
If str1 is less than str2, return -1
Natural sorting and dictionary sorting
Dictionary Sorting: Compare byte by byte according to byte ASCII
Natural sorting: According to human thinking, for example, "2">"11" in byte sorting, while "2"< "11"
Among the previous four comparison functions, you only need to understand what these two sortings are, and you can distinguish the four functions clearly. To give the most intuitive example: comparison of
hello11 and hello2, in dictionary sorting, hello11
<?php $str1 = 'hello11'; $str2 = 'hello2'; $str3 = 'Hello11'; $str4 = 'Hello2'; echo strcmp($str1,$str2).'<br>'; //-1 echo strcasecmp($str1,$str3).'<br>'; //0 echo strnatcmp($str1,$str2).'<br>'; //1 echo strnatcasecmp($str2,$str4).'<br>'; //0
The above is the detailed content of PHP determines whether strings are equal. For more information, please follow other related articles on the PHP Chinese website!