Home >Backend Development >PHP Tutorial >strncmp() function in PHP

strncmp() function in PHP

WBOY
WBOYforward
2023-08-19 15:33:111434browse

strncmp() function in PHP

strncmp() function is used to compare the first n characters.

Note - This function is case sensitive.

Syntax

strncmp(str1, str2, len)

Parameters

  • ##str1 − The first string

  • str2 − The second string

  • len − The number of characters used for comparison.

Return value

strncmp() function returns −

  • 0 - If the two strings are equal

  • >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’s see another example −

Real time demonstration

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

Output

The following is the output−

-32

The above is the detailed content of strncmp() function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete