PHP strncmp() Function

Definition

The strncmp() function compares two strings case-sensitive for specified length.

Syntax

PHP strncmp() Function has the following syntax.

strncmp(string1,string2,length)

Parameter

ParameterIs Required Description
string1Required.First string to compare
string2Required. Second string to compare
lengthRequired. Number of characters from each string to be used in the comparison

Return

This function returns:

  • 0 - if the two strings are equal
  • <0 - if string1 is less than string2
  • >0 - if string1 is greater than string2

Example 1

Compare two strings (case-sensitive):


<?php
echo strncmp("Hello world!","Hello earth!",6);
?>

The code above generates the following result.

Example 2

Compare two strings (case-sensitive = Hello and hELLo will not output the same):


<?php//from w  w w  .  jav  a2 s  . com
echo strncmp("Hello","Hello",6);
echo "<br>";
echo strncmp("Hello","hELLo",6);
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions