Concept
How can you compare strings by their content in PHP?
ComputerScienceOne / Arrays of Strings
"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.\n\n$x = strcmp(\"apple\", \"banana\"); //x is negative\n\n$x = strcmp(\"zelda\", \"mario\"); //x is positive"
Related Ideas
- How does PHP compare strings using strcmp()?ComputerScienceOne · Comparisons
- How does PHP compare strings with case-sensitive and case-insensitive functions?ComputerScienceOne · Tokenizing
- How does strcmp() compare the contents of two strings in C?ComputerScienceOne · Comparisons
- How do you compare strings in Java?ComputerScienceOne · Comparisons
- How can PHP arrays be sorted?ComputerScienceOne · Searching
- 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
- What are PHP strings and how should they be treated?ComputerScienceOne · Basics