In PHP, the strnatcmp function "natural sorting algorithm" is used to compare string usage analysis and strcmp function

Source: Internet
Author: User
: This article mainly introduces the strnatcmp function "natural sorting algorithm" in PHP to analyze the usage of string comparison and comparison strcmp functions. if you are interested in the PHP Tutorial, refer to it. This article describes how to use strnatcmp () function "natural sorting algorithm" in PHP to compare strings. We will share this with you for your reference. The details are as follows:

In PHP, the strnatcmp () function uses the "natural" algorithm to compare two strings (case sensitive). generally, in the natural algorithm, the number 2 is smaller than the number 10. In computer sorting, 10 is less than 2, because the first number in 10 is less than 2.

The strnatcmp () function is defined as follows:

Strnatcmp (string1, string2)

Parameter description:

String1 is required. Specifies the first string to be compared.
String2 is required. Specifies the second string to be compared.

Return value description:

If the two strings are equal, the return value is 0.
If string1 is less than string2, the return value is less than 0.
If string1 is greater than string2, the return value is greater than 0.

The sample code is as follows:

<? Php $ str1 = "2.jpg"; $ str2 =" 10.jpg"; $ str3 = "jb51.net _ 1"; $ str4 = "JB51.NET _ 2"; echo strcmp ($ str1, $ str2); // compare by byte, returns 1 echo"
"; Echo strcmp ($ str3, $ str4); // compare by byte, return 1 echo"
"; Echo strnatcmp ($ str1, $ str2); // compare by" natural sorting ", return-1 echo"
"; Echo strnatcmp ($ str3, $ str4); // compare by" natural sorting ", returns 1?>

The running result is as follows:

11-11

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.