PHP strncmp() Function (original) (raw)
Last Updated : 22 Jun, 2023
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,str1, str1,str2, $len )
Parameters: This function accepts three parameters as mentioned above and described below:
- $str1: It is mandatory parameter. This parameter refers to the first string to be used in the comparison.
- $str2: It is mandatory parameter. This parameter refers to the second string to be used in the comparison.
- $len: It is mandatory parameter which is used to define the first $len number of characters compared.
Return Value: This function returns a random integer value depending on the comparison of string which is given below:
- Returns 0 if the first n character of both strings are equal.
- Returns a negative value ( < 0), if the first n character of string2isgreaterthanstring2 is greater than string2isgreaterthanstring1.
- Returns a positive value ( > 0), if the first n character of string1isgreaterthanstring1 is greater than string1isgreaterthanstring2.
Below programs illustrate the strncmp() function in PHP.
Program 1:
PHP
<?php
$str1
=
"Welcome to GFG"
;
$str2
=
"Welcome to GeeksforGeeks"
;
$str3
=
"Welcome"
;
print_r(
strncmp
(
$str1
,
$str3
, 7));
echo
"\n"
;
print_r(
strncmp
(
$str2
,
$str1
, 14));
echo
"\n"
;
print_r(
strncmp
(
$str3
,
$str2
, 10))
?>
Program 2:
PHP
<?php
$str1
=
"GeeksforGeeks"
;
$str2
=
"geeksforgeeks"
;
print_r(
strncmp
(
$str1
,
$str2
, 13));
?>
Related Articles: