PHP strncasecmp Function

What is strncasecmp Function?

Explanation

In PHP this function is used for binary safe case-insensitive string comparison of the first n characters.

Syntax:


strncasecmp(string1,string2,length)

In the above syntax "string1" ,"string2" are the strings to compare, "length" specifies the number of characters from each string to be compared.The function returns "0" if both strings are equal, returns a value "<0" if string1 is less than string2, returns a value ">0" if string1 is greater than string2.

Example :


<?php
$arr1 = $arr2 = array("hi12", "hi10", "hi2", "hi1");
echo "Standard string comparisonn";
usort($arr1, "strcmp");
print_r($arr1);
echo "nNatural order string comparisonn";
usort($arr2, "strnatcmp");
print_r($arr2);
?>
Result :

Standard string comparison
Array
(
[0] => hi1
[1] => hi10
[2] => hi12
[3] => hi2
)
Natural order string comparison
Array
(
[0] => hi1
[1] => hi2
[2] => hi10
[3] => hi12
)

In the above strncasecmp function example the strings are sort by standard string comparison where 10 is greater than 2 and then using "strnatcmp" to display results.

PHP Topics


Ask Questions

Ask Question