In PHP, the strnatcmp () function "natural sorting algorithm" is used to analyze the usage of string comparison (compared with the strcmp function)

Source: Internet
Author: User
This article mainly introduces the use of strnatcmp () function "natural sorting algorithm" in PHP for string comparison, and analyzes the use techniques of strnatcmp function string comparison based on the instance form, the strcmp function is compared to demonstrate the differences between the two. For more information, see the example in this article. the strnatcmp () function "natural sorting algorithm" in PHP is used to compare strings. We will share this with you for your reference. The details are as follows:

PHPThe strnatcmp () function uses the "natural" algorithm to compare two strings (case sensitive). generally, in the natural algorithm, number 2 is smaller than 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.

Sample codeAs follows:

<? Php $ str1 = "2.jpg"; $ str2 =" 10.jpg"; $ str3 = "bitsCN.com _ 1"; $ str4 = "BITSCN. COM_2 "; echo strcmp ($ str1, $ str2); // compare by byte. 1 echo is returned"
"; 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

Related Article

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.