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 String values in Java?ComputerScienceOne · Tokenizing
- What are PHP strings and how should they be treated?ComputerScienceOne · Basics
- How can strtok() split a string into tokens?ComputerScienceOne · Tokenizing
- How can you extract substrings from a string in PHP?ComputerScienceOne · Comparisons