Home>

First, the strcmp () function in PHP is used to compare two strings (case sensitive), as defined below:

strcmp (string1, string2)

Parameter Description:

string1 is required. Specifies the first string to be compared.

string2 is required. Specifies the second string to be compared.

ps:The strcmp () function is similar to the strncmp () function.the difference is,strncmp () can specify the number of characters for each string to compare.

Second, the strcasecmp () function in PHP compares two strings (case-insensitive), which is defined as follows:

strcasecmp (string1, string2)

Parameter Description:

string1 is required. Specifies the first string to be compared.

string2 is required. Specifies the second string to be compared.

ps:The strcasecmp () function is similar to the strncasecmp () function.the difference is,strncasecmp () can specify the number of characters for each string to compare.

Sample code:

<?php
 echo strcmp ($str1, $str2);//Two strings are equal
 echo "<br />";
 echo strcmp ($str3, $str4);//Note that the function is case sensitive
 echo "<br />";
 echo strcasecmp ($str3, $str4);//The function is not case sensitive
?>

The results are as follows:

0
1
0

Added:Question about return value

Comparison of parameters 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

php
  • Previous Detailed JavaScript UTC time conversion method
  • Next PHP implementation of searching for geographic locations and calculating the distance between two geographic locations