strnatcmp is a string comparison function in PHP that compares two strings using natural order. The principle of natural order comparison is that numeric parts within strings are compared based on their numeric value rather than their literal character order.
<span><span><span class="hljs-title function_ invoke__">strnatcmp</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$str1</span></span><span>, </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$str2</span></span><span>): </span><span><span class="hljs-keyword">int</span></span></span>
$str1 and $str2 are the two strings to compare.
Return value: Returns a negative number if $str1 is less than $str2; returns 0 if they are equal; returns a positive number if $str1 is greater than $str2.
<span><span><span class="hljs-variable">$str1</span></span><span> = </span><span>"a10"</span></span>;
<span><span><span class="hljs-variable">$str2</span></span><span> = </span><span>"a2"</span></span>;
<p>echo strnatcmp($str1, $str2); // Outputs 1 because "a10" is naturally ordered after "a2"<br>
As shown, strnatcmp compares the numeric parts of strings as numbers instead of comparing character by character.
natsort is a PHP function used to sort arrays in natural order. Unlike strnatcmp, which compares two individual strings, natsort is a sorting function that directly sorts an entire array.
<span><span><span class="hljs-title function_ invoke__">natsort</span></span><span>(</span><span><span class="hljs-keyword">array</span></span><span> &</span><span><span class="hljs-variable">$array</span></span><span>): </span><span><span class="hljs-keyword">bool</span></span></span>
$array is the array to be sorted.
Return value: Returns true on success, false on failure.
<span><span><span class="hljs-variable">$array</span></span><span> = ["a10", "a2", "a1"];
<span><span><span class="hljs-title function_ invoke__">natsort</span></span><span>(</span><span><span class="hljs-variable">$array</span></span><span>);
<p><span class="hljs-title function_ invoke__">print_r($array<span>);<br>
Output:
<span><span><span class="hljs-title function_ invoke__">Array</span></span>
(
[2] => a1
[1] => a2
[0] => a10
)
It can be seen that natsort sorts the array elements in natural order. Its behavior is similar to strnatcmp but applies to arrays rather than individual strings.
Natural Ordering: Both strnatcmp and natsort follow natural ordering rules, especially when strings contain numbers.
Comparison Method for Numbers and Characters: Both treat numeric parts of strings as numbers instead of comparing character by character.
Scope of Application:
strnatcmp is used to compare the natural order of two strings.
natsort is used to sort an array in natural order.
Return Value:
strnatcmp returns an integer indicating the comparison result between two strings.
natsort sorts the array in place without returning the sorted array, directly modifying the original array.
Use Cases:
strnatcmp is suitable when you need to check whether two strings are ordered naturally.
natsort is suitable for naturally sorting arrays containing multiple strings.