Current Location: Home> Latest Articles> What Do Positive and Negative Return Values of strnatcmp in PHP Mean? How to Handle Them Correctly?

What Do Positive and Negative Return Values of strnatcmp in PHP Mean? How to Handle Them Correctly?

gitbox 2025-08-17

In PHP, the strnatcmp function is a tool used to compare two strings in natural order (also known as "natural sorting"). Unlike standard string comparison functions like strcmp, strnatcmp sorts based on numeric value, not just ASCII values of characters. Understanding how the function's positive and negative return values work, and how to handle them correctly, is crucial for developers.

What is the strnatcmp Function?

The prototype of strnatcmp is as follows:

<span><span><span class="hljs-keyword">int</span></span><span> </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>

This function takes two string parameters, $str1 and $str2, and returns an integer representing the result of their natural order comparison.

  • If $str1 is less than $str2, it returns a negative value;

  • If $str1 is equal to $str2, it returns 0;

  • If $str1 is greater than $str2, it returns a positive value.

How to Understand Positive and Negative Return Values?

  1. Negative Value:
    When strnatcmp returns a negative value, it means the first string $str1 comes before the second string $str2 in natural order. For example:

    <span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">strnatcmp</span></span><span>("apple10", "apple2");  </span><span><span class="hljs-comment">// Outputs a negative value</span></span><span></span>

    In this example, "apple10" comes before "apple2" in natural order because the number 10 is greater than 2.

  2. Zero:
    If the two strings are equal in natural order, strnatcmp returns 0. For example:

    <span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">strnatcmp</span></span><span>("apple2", "apple2");  </span><span><span class="hljs-comment">// Outputs 0</span></span><span></span>

    Here, the two strings are identical, so the function returns 0.

  3. Positive Value:
    When strnatcmp returns a positive value, it indicates that the first string $str1 comes after the second string $str2 in natural order. For example:

    <span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">strnatcmp</span></span><span>("apple20", "apple2");  </span><span><span class="hljs-comment">// Outputs a positive value</span></span><span></span>

    In this example, "apple20" comes after "apple2" because 20 is greater than 2.

How to Handle the Return Values of strnatcmp Correctly?

  1. Simple Comparison:
    If you only want to determine which string is greater, you can directly use the return value. For example:

    <span><span><span class="hljs-variable">$result</span></span><span> = </span><span><span class="hljs-title function_ invoke__">strnatcmp</span></span><span>($str1, $str2);
    <p></span>if ($result < 0) {<br>
    echo "$str1 comes before $str2";<br>
    } </span>elseif ($result > 0) {<br>
    echo "$str1 comes after $str2";<br>
    } </span>else {<br>
    </span>echo "$str1 and $str2 are the same";<br>
    }</span>

    This approach works well for sorting or comparison scenarios.

  2. Using in Sorting:
    strnatcmp is often used for naturally sorting arrays, especially when strings contain numbers. If you want to sort an array in natural order, you can combine it with usort:

    <span>$array = ["apple10", "apple2", "apple20", "apple1"];
    </span><span>usort($array, 'strnatcmp');
    </span><span>print_r($array);
    </span>

    This will output:

    <span>Array
    (
        [0] => apple1
        [1] => apple2
        [2] => apple10
        [3] => apple20
    )
    </span>
  3. Handling Complex Cases:
    Sometimes you need to compare more complex strings, such as those containing dates or version numbers. In such cases, strnatcmp can help compare them in natural order without incorrectly comparing numeric parts as characters.

    <span>$result = strnatcmp("version 10.1", "version 9.9");
    </span><span>if ($result > 0) {
        </span><span>echo "version 10.1 is newer than version 9.9";
    }
    </span>

    The output will be: version 10.1 is newer than version 9.9, showing that strnatcmp correctly handles version numbers in natural order.

Conclusion

strnatcmp is a very useful PHP function that compares strings using natural sorting rules. Especially when dealing with strings containing numbers, it provides a sorting method that aligns better with human intuition. Understanding the meaning of its positive and negative return values and handling them correctly helps developers perform string comparisons and sorting more efficiently.