strncmp() function in PHP


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 26-Dec-2019

145 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements