php The strcasecmp function is used to compare two strings. Its syntax is strcasecmp(string1,string2). The parameter string1 is required and refers to the first string to be compared; string2 is required and refers to the second string to be compared. String.
#php How to use strcasecmp function?
Definition and usage
strcasecmp() function compares two strings.
Tip: The strcasecmp() function is binary safe and case-insensitive.
Tip: This function is similar to the strncasecmp() function, except that with strncasecmp() you can specify the number of characters for each string to be compared.
Syntax
strcasecmp(string1,string2)
Parameters
string1 Required. Specifies the first string to compare.
string2 Required. Specifies the second string to be compared.
Return value:
This function returns:
● 0 - If the two strings are equal
● <0 - If string1 is less than string2
● >0 - If string1 is greater than string2
PHP version: 4
Example 1
Compare two strings ( Not case sensitive, HELLO and hELLo output the same):
<?php echo strcasecmp("Shanghai","SHANGHAI"); echo "<br>"; echo strcasecmp("Shanghai","sHANGHai"); ?>
Output:
0 0
Example 2
Different return values:
<?php echo strcasecmp("Hello world!","HELLO WORLD!"); // 两字符串相等 echo strcasecmp("Hello world!","HELLO"); // string1 大于 string2 echo strcasecmp("Hello world!","HELLO WORLD! HELLO!"); // string1 小于 string2 ?>
Output:
0 7 -7
Example 3
Compare two strings (case-insensitive):
<?php echo strcasecmp("shanghai","SHANGHAI"); ?>
Output:
0
The above is the detailed content of How to use php strcasecmp function?. For more information, please follow other related articles on the PHP Chinese website!