The difference between PHP strcmp,strnatcmp,strncmp functions

Source: Internet
Author: User
Tags strcmp

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

1<?PHP2 $str 1= ' str100 ';3 $str 2= ' Str20 ';4 Echo strcmp($str 1,$str 2)." <br> ";5 //Output-16 Echo strnatcmp($str 1,$str 2)." <br> ";7 //Output18 Echo strncmp($str 1,$str 2, 5). " <br> ";9 //Output-1Ten  One?>

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.

The difference between PHP strcmp,strnatcmp,strncmp functions

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.