strcmp() do?strcmp() compares two strings exactly and returns:
- 0 if both strings are equal
- A negative number if the first string is less than the second
- A positive number if the first string is greater than the second
strcasecmp() different from strcmp()?strcasecmp() compares two strings ignoring case differences, while strcmp() is case-sensitive.
strncmp() do in PHP?strncmp() compares up to a specified number of characters from two strings. It returns 0 if the compared parts are equal.
strcmp() with an example.Example: strcmp('apple', 'banana') returns a negative number because 'apple' is less than 'banana' alphabetically.
Return values:
- 0 if equal
- <0 if first string < second string
- >0 if first string > second string
strcasecmp() instead of strcmp()?Use strcasecmp() when you want to compare strings without caring about uppercase or lowercase letters, like comparing user input where case should not matter.
strcmp('Hello', 'hello') return?strcmp() is case-sensitive. 'H' (uppercase) is less than 'h' (lowercase) in ASCII, so it returns a negative number.
strcasecmp() compares strings ignoring case differences.
strncmp('apple', 'apricot', 3) return?It compares first 3 characters: 'app' vs 'apr'. Since 'app' and 'apr' differ at the third character, 'p' vs 'r', 'p' is less than 'r', so it returns a negative number.
strncmp() compares up to N characters of two strings.
strcmp($a, $b) returns 0, what does it mean?A return value of 0 means both strings are exactly equal.
strcmp() works and what its return values mean.strcasecmp() instead of strcmp()?