Concept
How does PHP compare strings using strcmp()?
ComputerScienceOne / Comparisons
"43.4. Comparisons When comparing strings in PHP, we can use the usual comparison operators such as === , < , or <= which will compare the strings lexicographically. However, this is generally discouraged because of type juggling issues and strict vs loose equality/inequality comparisons. Instead, there are several comparator methods that PHP provides to compare strings based on their content. strcmp($a, $b) takes two strings and returns an integer based on the lexicographic ordering of $a and $b . If $a precedes $b , strcmp() returns something negative. It returns zero if $a and $b have the same content. Otherwise it returns something positive if $b precedes $a . 1 $x = strcmp(\"apple\", \"banana\"); //x is negative 2 $x = strcmp(\"zelda\", \"mario\"); //x is positive 560"
Related Ideas
- How can you compare strings by their content in PHP?ComputerScienceOne · Arrays of Strings
- How does strcmp() compare the contents of two strings in C?ComputerScienceOne · Comparisons
- How does PHP compare strings with case-sensitive and case-insensitive functions?ComputerScienceOne · Tokenizing
- How do you compare strings in Java?ComputerScienceOne · Comparisons
- Why can’t numerical comparison operators be used to compare strings in C?ComputerScienceOne · Comparisons
- How can comparator functions order Student structures?ComputerScienceOne · Comparator Functions
- What is a comparator function and how does it determine element order?ComputerScienceOne · Comparator Functions
- How can PHP arrays be sorted?ComputerScienceOne · Searching