This article analyses the usage of PHP string comparison function. Share to everyone for your reference, specific as follows:
You can use "= =" to compare strings directly, but sometimes you might want to do more complex string comparisons, such as partial matching.
1.STRCMP () function: This function is a comparison between strings, and is case-sensitive when compared.
Statement: strcmp (string str1,string str2)
The function compares the two string parameters passed in and returns 0 if the two strings are identical, returns a positive number if the str1 is followed by a dictionary order, and returns a negative number if str1 is less than str2.
Cases:
<?php
$a = "I want to fly";
$b = "I want to climb";
$back = strcmp ($a, $b);
if ($back >0)
echo ' $a greater than $b ';
ElseIf ($back <0)
Echo ' $a less than $b ';
else
echo ' $a equals $b ';
? >
Output results:
$a greater than $b
2.STRCASECMP (): This function is basically consistent with the strcmp function, but the function is case-insensitive when compared.
Cases:
<?php
$val 1 = "Hello";
$val 2 = "Hello";
if (strcasecmp ($val 1, $val 2) ==0)
Echo ' $val 1 and $val2 the same (ignoring the case of strings) ';
? >
Output results:
$val 1 is the same as $VAL2 (ignores the case of strings)
3. Natural sort strnatcmp (): This function is basically consistent with the usage of strcmp function, but the principle of comparison is different . This function is not arranged in dictionary order, but is sorted by "natural sort" Compare strings. The so-called natural sort is based on people's habits to sort, such as strcmp function to sort, "4" will be greater than "14", and in reality, the number "14" in Greater than "4", so the STRNATCMP function is based on the latter to compare.
4.STRNATCASECMP (): This function is consistent with the usage of the STRCASECMP function, except that the function is case-insensitive
For more information about PHP interested readers can view the site topics: "PHP string (String) Usage summary", "PHP Mathematical Arithmetic Skills summary", "PHP object-oriented Programming Introduction Course", "PHP Array" Operation Techniques Encyclopedia, " PHP Data structure and algorithm tutorial, "PHP Programming Algorithm Summary", "PHP Regular Expression Usage Summary", and "PHP Common database Operation Skills Summary"
I hope this article will help you with the PHP program design.