Home >Backend Development >PHP Tutorial >Analysis on string comparison usage of strcmp() and strcasecmp() functions in PHP, strcmpstrcasecmp_PHP tutorial
The examples in this article describe the comparison usage of strcmp() and strcasecmp() function strings in PHP . Share it with everyone for your reference, the details are as follows:
1. The strcmp() function in PHP is used to compare two strings (case sensitive) , and its definition is as follows:
strcmp(string1,string2)
Parameter description:
string1 Required. Specifies the first string to compare.
string2 required. Specifies the second string to be compared.
PS: The strcmp() function is similar to the strncmp() function. The difference is that strncmp() can specify the number of characters for each string to be compared.
2. The strcasecmp() function in PHP compares two strings (case-insensitive) , which is defined as follows:
strcasecmp(string1,string2)
Parameter description:
string1 required. Specifies the first string to compare.
string2 required. Specifies the second string to be compared.
PS: The strcasecmp() function is similar to the strncasecmp() function. The difference is that strncasecmp() can specify the number of characters for each string to be compared.
Sample code:
<?php $str1="帮客之家"; $str2="帮客之家"; $str3="www.jb51.net"; $str4="WWW.JB51.NET"; echo strcmp($str1,$str2);//两个字符串相等 echo "<br/>"; echo strcmp($str3,$str4);//注意该函数区分大小写 echo "<br/>"; echo strcasecmp($str3,$str4);//该函数不区分大小写 ?>
The running results are as follows:
0 1 0
Supplement: Regarding the return value issue
Comparison of parameters string1 and string2:
If the two strings are equal, the return value is 0
If string1 is less than string2, the return value is less than 0
If string1 is greater than string2, the return value is greater than 0
I hope this article will be helpful to everyone in PHP programming.