• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer navigation

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • VCS
  • Interview Questions
  • Database
    • MariaDB
  • DevOps
    • Docker
    • Shell Scripting
  • Big Data
    • Hadoop
    • Cloudera
    • Hortonworks HDP

PHP Function strncasecmp() – Binary safe case-insensitive string comparison of the first n characters

by admin

The strncasecmp() function is used to compare a case-sensitive string of the first ‘n’ character. This function was introduced in PHP4. The comparison is case-insensitive—that is, “Alphabet” and “alphabet” are considered equal. This function is a case-insensitive version of strcmp(). If either string is shorter than length characters, the length of that string determines how many characters are compared.

Syntax:

strncasecmp(string $string1, string $string2, int $length): int

Parameters

Parameter Description
string1 The first string.
string2 The second string.
length The length of strings to be used in the comparison.

Return Values

Returns < 0 if string1 is less than string2; > 0 if string1 is greater than string2, and 0 if they are equal.

Examples

$var1 = 'Hello John';
$var2 = 'hello Doe';
if (strncasecmp($var1, $var2, 5) === 0) {
    echo 'First 5 characters of $var1 and $var2 are equals in a case-insensitive string comparison';
}

Final Thoughts

Use strncasecmp to compare the first parts of two strings. PHP compares the strings, character by character, until comparing the number of characters specified by length or reaching the end of one of the strings. PHP treats letters of different case as equal. If first and second are equal, PHP returns zero. If first comes before second, PHP returns a negative number. If second comes before first, PHP returns a positive number.

Filed Under: PHP

Some more articles you might also be interested in …

  1. PHP atan2 function – Arc tangent of two variables
  2. PHP asin function – Arc sine
  3. PHP function str_repeat() – Repeat a string
  4. PHP Function strchr() – Alias of strstr()
  5. PHP function acos – Arc cosine
  6. PHP Function str_word_count() – Return information about words used in a string
  7. PHP base_convert function – Convert a number between arbitrary bases
  8. PHP cos function – Cosine
  9. PHP function abs – Absolute value
  10. PHP function strcasecmp() – Binary safe case-insensitive string comparison

You May Also Like

Primary Sidebar

Recent Posts

  • Vanilla OS 2 Released: A New Era for Linux Enthusiasts
  • mk Command Examples
  • mixxx Command Examples
  • mix Command Examples

© 2025 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright