Open In App

PHP strncmp() Function

The strncmp() is an inbuilt function in PHP which is used to compare first n character of two strings. This function is case-sensitive which points that capital and small cases will be treated differently, during comparison. This function compares two strings with first n character and tells whether the first string is greater or smaller or equal to the second string.

int strncmp( $str1, $str2, $len )

Parameters: This function accepts three parameters as mentioned above and described below: 



Return Value: This function returns a random integer value depending on the comparison of string which is given below: 

Below programs illustrate the strncmp() function in PHP.



Program 1:  




<?php
  
// PHP program to illustrate the working of strncmp()
$str1 = "Welcome to GFG";
$str2 = "Welcome to GeeksforGeeks";
$str3 = "Welcome";
  
// In this case both the strings are equal
print_r(strncmp($str1, $str3, 7));
echo "\n";
  
// In this case the first is greater
print_r(strncmp($str2, $str1, 14));
echo "\n";
  
// In this case the second is greater
print_r(strncmp($str3, $str2, 10))
  
?>

Output: 
0
31
-3




 

Program 2: 




<?php
  
// PHP program to illustrate the working of strncmp()
$str1 = "GeeksforGeeks";
$str2 = "geeksforgeeks";
  
// In this case both the strings are equal
print_r(strncmp($str1, $str2, 13));
  
?>

Output: 
-32




 

Related Articles: 

Reference: http://php.net/manual/en/function.strncmp.php
 


Article Tags :