Example
Compare two strings (case-insensitive):
<?php echo strcasecmp("Hello world!","HELLO WORLD!"); ?>
Definition and usage
strcasecmp() Function Compares two strings.
Tip: The strcasecmp() function is binary safe and is not case-sensitive.
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 | Description |
string1 | Required. Specifies the first string to compare. |
string2 | Required. Specifies the second string to be compared. |
Technical details
Return value: | This function returns:
|
4+ |
Instance 1
Compare two strings (not case sensitive, HELLO and hELLo output the same):
<?php echo strcasecmp("Hello","HELLO"); echo "<br>"; echo strcasecmp("Hello","hELLo"); ?>
Instance 2
Different Return value:
<?php echo strcasecmp("Hello world!","HELLO WORLD!"); // The two strings are equal echo strcasecmp("Hello world!","HELLO"); // String1 is greater than string2 echo strcasecmp("Hello world!","HELLO WORLD! HELLO!"); // String1 is less than string2 ?>
strcasecmp — Binary safe comparison of strings (case-insensitive)
int strcasecmp ( string $str1 , string $str2 )
Return value:
//如果 str1 小于 str2,返回负数;如果 str1 大于 str2,返回正数;二者相等则返回 0。
Simple example:
<?php $var1 = "Hello"; $var2 = "hello"; if (strcasecmp($var1, $var2) == 0) { echo '$var1 is equal to $var2 in a case-insensitive string comparison'; } ?>
The above is the detailed content of PHP function strcasecmp() that compares two strings. For more information, please follow other related articles on the PHP Chinese website!