PHP | strnatcasecmp() Function

The strnatcasecmp() function is a built-in function in PHP which compares this string using “natural order” algorithm. This function accepts two strings as parameter and returns an integer value (positive, negative or zero ). This function is similar to strnatcmp() with the only difference being the case-insensitivity of the function.

Syntax:

strnatcasecmp( $string1, $string2 )

Parameters: The function accepts two mandatory string parameters as shown in the above syntax. These parameters are defined below :



  • $string1: This parameter specifies the first string to be compared.
  • $string2: This parameter specifies the second string to be compared.

Return Values: This function returns a positive integer, negative or 0 based on the below conditions:

  • Returns 0 if the two strings are equal
  • Returns a positive value (>0) if the $string1 is greater than $string2.
  • Returns a negative value (<0) if the $string1 is less than $string2.

Examples:

Input : $string = "Geek", $string2 = "GEEK"
Output : 0

Input : $string = "Geeks", $string2 = "Geek"
Output : 1

Below programs illustrate the strnatcasecmp() function:

Program 1: This program illustrates simple use of the strnatcasecmp() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
echo strnatcasecmp("Geeks", "Geek");
  
?>

chevron_right


Output

1

Program 2: This program illustrates case-insensitivity of the strnatcasecmp() function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Case-insensitive strnatcasecmp() function
echo strnatcasecmp("Geeks", "GEEKS");
echo "\n";
  
// Case-sensitive strnatcmp() function
echo strnatcmp("Geeks", "GEEKS");
  
?>

chevron_right


Output

0
1


My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : nidhi_biet



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.