Home>

Directly compare whether the strings are exactly the same,You can use "==" to do this, but sometimes more complex string comparisons may be required,Such as partial matching.

1.strcmp () function:This function compares strings.When comparing,Case sensitive.

Statement:strcmp (string str1, string str2)

This function compares the two string parameters passed in,If the two strings are exactly the same,Returns 0;if str1 follows str2 in lexicographic order, returns a positive number;If str1 is less than str2, a negative number is returned.

example:

<?php
 $a="i want to fly";
 $b="i want to climb";
 $back=strcmp ($a, $b);
 if ($back>0)
  echo "$a is greater than $b";
 elseif ($back<0)
  echo "$a is less than $b";
 else
  echo "$a equals $b";
?>

Output results:

$a is greater than $b

2.strcasecmp ():This function is basically the same as the strcmp function.But when the function is compared,not case sensitive.

example:

<?php
  $val1="hello";
  $val2="hello";
  if (strcasecmp ($val1, $val2) == 0)
  echo "$val1 and $val2 are the same (ignore the case of the string)";
?>

Output results:

$val1 and $val2 are the same (ignore the case of the string)

3. Natural order strnatcmp ():This function is basically the same as the strcmp function,But the principle of comparison is all different. The function is not in lexicographic order,Instead, strings are compared in "natural order".The so-called natural sort is to sort according to people's habits.E.g. strcmp function to sort,"4" would be greater than "14", and in reality,The number "14" is greater than "4", so the strnatcmp function is compared according to the latter.

4.strnatcasecmp ():This function is consistent with the use of strcasecmp function,Just the function is not case sensitive

php
  • Previous Analysis of set_include_path () function usage in PHP
  • Next Another wonderful barrage effect jQuery implementation