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.
PS: Thestrcmp () 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.
PS: Thestrcasecmp () function is similar to the strncasecmp () function, and strncasecmp () can specify the number of characters per string to compare.
Sample code:
<?php
$str 1 = "cloud-dwelling community";
$str 2 = "cloud-dwelling community";
$str 3= "Www.jb51.net";
$str 4= "WWW.JB51.NET";
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
?>
The results of the operation are as follows:
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
I hope this article will help you with the PHP program design.