English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP strnatcasecmp() Function Usage and Example

PHP String Manual

The strnatcasecmp() function uses the "natural order" algorithm to compare strings (case-insensitive).

Syntax

strnatcasecmp(string1,string2)

Definition and Usage

It uses a natural algorithm to compare two strings. This function implements a comparison algorithm for sorting numeric strings in human habit.

In natural algorithms, numbers 5 Less than a number 10. In computer sorting,10 Less than 5This is because 10 The first number is less than 5.

Return Value

 Similar to other string comparison functions, if string1 Less than string2 Returns < 0; if string1 Greater than string2 Returns > 0; if they are equal, returns 0.

Parameter

NumberParameters and Description
1

string1

The first string

2

string2

The second string

Online Example

Try the following example, strnatcasecmp uses a "natural" algorithm to compare two strings (case-insensitive):

<?php
//Case-insensitive comparison of two strings
echo strnatcasecmp("5w3codebox!"5w3codebox!');
echo "<br>";
//The second string is greater than the first string
echo strnatcasecmp("5w3codebox!"58w3codebox!');
echo "<br>";
//The second string is less than the first string
echo strnatcasecmp("101111w3codebox!"211w3codebox!');
?>
Test and see‹/›

Output Result

0
-1
1

PHP String Manual