strncmp() function in PHP

PHPProgrammingServer Side Programming

The strncmp() function is used to compare first n character.

Note − The function is case-sensitive.

Syntax

strncmp(str1, str2, len)

Parameters

  • str1 − The first string

  • str2 − The second string

  • len − The number of characters to use in the comparison.

Return

The strncmp() function returns −

  • 0 - if the two strings are equal

  • <0 - if string1 is less than string2

  • >0 - if string1 is greater than string2

Example

The following is an example −

 Live Demo

<?php
   echo strncmp("Demo text!","DEMO word!",4);
?>

Output

The following is the output −

8192

Example

Let us see another example −

 Live Demo

<?php
   $str1 = "TomHanks";
   $str2 = "tomhanks";
   print_r(strncmp($str1, $str2, 8));
?>

Output

The following is the output −

-32
raja
Published on 17-Oct-2018 12:54:06

Advertisements