Concept
How does PHP compare strings with case-sensitive and case-insensitive functions?
ComputerScienceOne / Tokenizing
"strcmp() compares two strings and returns zero when they are equal. Shorter strings precede longer strings, so strcmp(\"apple\", \"apples\") returns a negative value. strcmp(\"Apple\", \"apple\") also returns a negative value because uppercase letters are ordered before lowercase letters according to the ASCII table. Case-insensitive comparisons can be performed with strcasecmp($a, $b); strcasecmp(\"Apple\", \"apple\") returns zero because the two strings are the same when case is ignored. The comparison functions also have length-limited versions, strncmp($a, $b, $n) and strncasecmp($a, $b, $n). Both only compare the first $n characters of the strings. Thus, strncmp(\"apple\", \"apples\", 5) returns zero because the two strings are equal in their first five characters.\n\n$x = strcmp(\"Hello\", \"Hello\"); // x is zero\n\n$x = strcmp(\"apple\", \"apples\"); // x is negative\n\n$x = strcmp(\"Apple\", \"apple\"); // x is negative"
Related Ideas
- How does PHP compare strings using strcmp()?ComputerScienceOne · Comparisons
- How can you compare strings by their content in PHP?ComputerScienceOne · Arrays of Strings
- How do you compare strings in Java?ComputerScienceOne · Comparisons
- How can comparison operators be used with characters in C?ComputerScienceOne · Comparing Strings and Characters
- What is the order of precedence for PHP operators?ComputerScienceOne · If, If-Else, If-Else-If Statements
- How does strcmp() compare the contents of two strings in C?ComputerScienceOne · Comparisons
- How can PHP arrays be sorted with sort() and usort()?ComputerScienceOne · Sorting
- What is a comparator function and how does it determine element order?ComputerScienceOne · Comparator Functions