Home  >  Article  >  Backend Development  >  How to use strnatcmp function

How to use strnatcmp function

藏色散人
藏色散人Original
2019-02-01 09:47:532140browse

PHP strnatcmp function: Compare strings using natural sorting algorithm.

How to use strnatcmp function

php strnatcmp() function syntax

Function: compare strings in natural order

Syntax:

strnatcmp(string1,string2)

Parameters:

string1 Required, specifies the first string to be compared.

string2 must specify the second string to be compared.

Description: The strnatcmp() function uses a "natural" algorithm to compare two strings. In natural arithmetic, the number 2 is less than the number 10. In computer sorting, 10 is less than 2 because the first number in 10 is less than 2. Note that this function is case-sensitive. If the two strings are equal, 0 is returned. If string1 is greater than string2, the returned value is greater than 0. If string1 is less than string2, the returned value is less than 0.

php strnatcmp() function usage example:

<?php
//使用自然算法来比较两个字符串
echo strnatcmp("2hello world!","01hello world!");
echo "<br>";
echo strnatcmp("01hello world!","2hello world!");
?>

Output:

1
-1

This article is an introduction to the PHP strnatcmp function. I hope it will be helpful to friends in need!

The above is the detailed content of How to use strnatcmp function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn