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

The Geek Diary

  • OS
    • Linux
    • CentOS/RHEL
    • Solaris
    • Oracle Linux
    • VCS
  • Interview Questions
  • Database
    • oracle
    • oracle 12c
    • ASM
    • mysql
    • 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 cos function – Cosine
  2. PHP asin function – Arc sine
  3. PHP function str_replace() – Replace all occurrences of the search string with the replacement string
  4. PHP function chr() – Generate a single-byte string from a number
  5. PHP function chop() – Alias of rtrim()
  6. PHP function chunk_split() – Split a string into smaller chunks
  7. PHP function abs – Absolute value
  8. PHP bindec function – Convert a number between arbitrary bases
  9. PHP atan2 function – Arc tangent of two variables
  10. PHP function stripcslashes() – Un-quote string quoted with addcslashes()

You May Also Like

Primary Sidebar

Recent Posts

  • bat: Print and concatenate files
  • bastet: Clone of the game Tetris in the terminal
  • bashmarks: Save and jump to commonly used directories using 1 character commands
  • bash Command Examples

© 2023 · The Geek Diary

  • Archives
  • Contact Us
  • Copyright