Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP String strncmp() Function



The PHP String strncmp() function is used to compare the first n characters of two strings. This function is case-sensitive, which means that capital and small cases will be treated differently when compared.

This function compares two strings using the first n characters and returns if the first string is larger, smaller, or equal to the second string.

Syntax

Below is the syntax of the PHP String strncmp() function −

int strncmp ( string $string1, string $string2, int $length )

Parameters

Here are the parameters of the strncmp() function −

  • $string1 − (Required) This argument specifies the first string to be used in the comparison.

  • $string2 − (Required) This argument specifies the second string to be used in the comparison.

  • $length − (Required) It is used to specify the initial $length number of characters compared.

Return Value

The strncmp() function returns -1 if string1 is less than string2, 1 if string1 is greater than string2, and zero if they are equal.

PHP Version

First introduced in core PHP 4, the strncmp() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

First we will show you the basic example of the PHP String strncmp() function to compare the first six characters of two strings.

<?php
   echo strncmp("input","input",6);
   echo "\n";

   echo strncmp("input","iMPut",6);
?>

Output

Here is the outcome of the following code −

0
33

Example 2

In the below PHP code we will use the strncmp() function and compare the first n characters of two strings to see if they are equal.

<?php
   // Define strings here
   $string1 = "apple";
   $string2 = "apricot";
   $length = 3;

   $result = strncmp($string1, $string2, $length);

   if ($result === 0) {
      echo "The first $length characters of the strings are equal.";
   } elseif ($result < 0) {
      echo "String1 is less than String2.";
   } else {
      echo "String1 is greater than String2.";
   }
?> 

Output

This will generate the below output −

String1 is less than String2

Example 3

Now the below code shows that strncmp() function is case-sensitive by comparing strings with different cases.

<?php
   // Define strings here
   $string1 = "HelloWorld";
   $string2 = "helloworld";
   $length = 5;

   $result = strncmp($string1, $string2, $length);

   if ($result === 0) {
      echo "The first $length characters of the strings are equal.";
   } else {
      echo "The first $length characters are not equal (case-sensitive comparison).";
   }
?> 

Output

This will create the below output −

The first 5 characters are not equal (case-sensitive comparison).

Example 4

In the following example, we are using the strncmp() function to compare parts of two longer strings by selecting substrings.

<?php
   // Define strings here
   $string1 = "Welcome to PHP Programming";
   $string2 = "Welcome to Python Programming";
   $length = 11;

   $result = strncmp($string1, $string2, $length);

   if ($result === 0) {
      echo "The first $length characters of both strings are the same.";
   } elseif ($result < 0) {
      echo "String1 is lexically less than String2 in the first $length characters.";
   } else {
      echo "String1 is lexically greater than String2 in the first $length characters.";
   }
?> 

Output

Following is the output of the above code −

The first 11 characters of both strings are the same.
php_function_reference.htm
Advertisements