PHP strcmp,strnatcmp,strncmp function differences, strnatcmpstrncmp_php tutorial

Source: Internet
Author: User
Tags strcmp

The difference between PHP strcmp,strnatcmp,strncmp functions, strnatcmpstrncmp


string comparison description
When S1<>< span="">
When s1=s2, return value = 0
Returns a positive number when S1>S2 <>

 
 1 
   PHP 2$str 1 = ' str100 '; 3 $str 2 = ' Str20 '; 4 Echo strcmp ($str 1$str 2). "
"; 5 //Output -1 6 Echo strnatcmp ($str 1$str 2). "
"; 7 //Output 1 8 Echo strncmp ($str 1$str 2, 5). "
"; 9 //Output -1 Ten ?>

The strcmp (str1, str2) function compares two strings by character, that is, two strings are compared from left to right by character (compared to the ASCII value size) until a different character occurs or a '/s ' is encountered. such as: "A" < "B" "a" > "a" "Computer" > "Compare"

The strnatcmp (str1, str2) function uses a natural sorting algorithm to compare strings This function implements a comparison algorithm that is used to sort numeric strings by human habit, which is the "natural order".

The principle of strncmp (str1, str2, len) function comparison refers to the strcmp (str1, str2) function, except that only the Len characters at the beginning of the string are compared.

PS: The above three functions are case-sensitive, if the comparison of the string is not case-sensitive, there are another three similar functions: strcasecmp,strnatcasecmp,strncasecmp.


string comparison function strcmp () and strncmp ()

Compare:
Function prototypes: extern int strcmp (const char *s1,const char * s2);
int strncmp (char *str1, char *str2, int maxlen);
Can be seen strncmp more than one parameter maxlen, the difference is here, this represents the comparison string of the first maxlen length of the character

The return values of the two are also different:
strcmp, when S1<>
When S1==s2, returns 0
Returns a positive number when S1>S2
STRNCMP: If the former maxlen byte is exactly equal, the return value is = 0;
Returns (Str1[n]-str2[n]) if str1[n] and str2[n] are not equal during the previous MaxLen byte comparison.

string comparison function strcmp () and strncmp ()

function: int strcmp (const char *S1, const char *S2)
This function is used to compare S1 and S2 strings, and this function returns a value whose symbol is related to the first comparison of different characters.
If two strings are equal, strcmp will return 0.
If S1 is a substring of S2, S1 is less than S2
There are also functions
int strncmp (const char *S1, const char *S2, size_t size)
This function is very similar to strcmp. The difference is that the STRNCMP function is a specified comparison of size characters. That is, if the string S1 is the same as the first size character of S2, the function returns a value of 0.

http://www.bkjia.com/PHPjc/887684.html www.bkjia.com true http://www.bkjia.com/PHPjc/887684.html techarticle php strcmp,strnatcmp,strncmp function differences, strnatcmpstrncmp string comparison shows that when S1s2 returns negative when S1=S2, return value = 0 when s1s2, return a positive number 1? PHP 2 $st ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.