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.
<?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"; ?>
$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.
<?php $val1="hello"; $val2="hello"; if (strcasecmp ($val1, $val2) == 0) echo "$val1 and $val2 are the same (ignore the case of the string)"; ?>
$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
- Examples of common PHP string processing functions [transform, escape, intercept, compare, find, reverse, cut]
- Summary of PHP's commonly used String function examples [conversion, replacement, calculation, interception, encryption]
- Usage and difference of replace string functions strtr () and str_repalce () in php
- PHP use strstr () function to get all characters after the specified string
- Summary of PHP common character function examples
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- i want to call a child component method from a parent in vuejs
- xcode - pod install [!] no `podfile 'found in the project directory
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file