Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the php string comparison function

Shulou Source: shulou.com Published: 2022-06-03 19:35:42 09月19日 Update

This article mainly introduces how to use the php string comparison function, which is very detailed and has a certain reference value. Friends who are interested must finish it!

1.strcmp () function: this function compares strings and is case-sensitive when comparing.

Declaration: strcmp (string str1,string str2)

This function compares the two passed string arguments and returns 0 if the two strings are exactly the same, a positive number if str1 follows str2 in dictionary order, and a negative number if str1 is less than str2.

Example:

Output result:

$an is greater than $b

2.strcasecmp (): this function is basically the same as the strcmp function, but when compared, it is not case-sensitive.

Example:

Output result:

$val1 is the same as $val2 (ignore the case of the string)

3. Natural sort strnatcmp (): this function is basically the same as the strcmp function, but the principle of comparison is different. This function is not arranged in dictionary order, but compares strings in "natural sort". The so-called natural sorting is to sort according to people's habits, such as the strcmp function to sort, "4" will be greater than "14", but in reality, the number "14" is greater than "4", so the strnatcmp function is compared according to the latter.

4.strnatcasecmp (): this function is the same as the strcasecmp function, except that it is not case-sensitive

The above is all the contents of the article "how to use the php string comparison function". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Function character string sort size consistent natural same two content dictionary time article result order output difference between people value Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Shulou Tech Info Apple MariaDB OPPO Reno