This article mainly introduced PHP strcmp () and strcasecmp () function string comparison usage, combined with the case form in detail analysis of the strcmp () and strcasecmp () function, the use of methods and differences
The examples in this article describe the comparison usage of strcmp () and strcasecmp () function strings in PHP. Share to everyone for your reference, specific as follows:
The strcmp () function in PHP is used to compare two strings (case sensitive), which is defined as follows:
strcmp (STRING1,STRING2)
Parameter description:
String1 required. Specify the first string to compare.
string2 required. Specify the second string to compare.
The ps:strcmp () function is similar to the strncmp () function, and strncmp () can specify the number of characters per string to compare.
The strcasecmp () function in PHP compares two strings (case-insensitive), which is defined as follows:
STRCASECMP (STRING1,STRING2)
Parameter description:
String1 required. Specify the first string to compare.
string2 required. Specify the second string to compare.
The ps:strcasecmp () function is similar to the strncasecmp () function, and strncasecmp () can specify the number of characters per string to compare.
Sample code:
<?php
$str 1 = "Code Agriculture Tutorial";
$str 2 = "Code agriculture Tutorial";
$str 3= "www.jmanongjc.com";
$str 4= "Www.jmanongjc.COM";
Echo strcmp ($str 1, $str 2);//Two strings equal
echo "<br/>";
Echo strcmp ($str 3, $str 4);//Note that the function is case-sensitive and case
echo "<br/>";
Echo strcasecmp ($str 3, $str 4);//This function is case-insensitive
?>
Results:
"; Echo strcmp ($str 3, $str 4);//Note that the function is case-sensitive echo "
"; Echo strcasecmp ($str 3, $str 4);//The function is case-insensitive?>
Add: About return value issues
For the comparison of parameter string1 and string2:
If 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
Thank you for reading, I hope to help you, thank you for your support for this site!