String comparison functions help you check if two pieces of text are the same or which one comes first in order. This is useful when sorting or checking user input.
String comparison functions in PHP
strcmp(string $str1, string $str2): int strcasecmp(string $str1, string $str2): int strncmp(string $str1, string $str2, int $length): int strncasecmp(string $str1, string $str2, int $length): int
These functions return 0 if the strings are equal.
A negative number means the first string is less than the second; a positive means it is greater.
$result = strcmp("apple", "banana"); // $result will be negative because "apple" comes before "banana"
$result = strcasecmp("Apple", "apple"); // $result will be 0 because comparison ignores case
$result = strncmp("applepie", "applesauce", 5); // Compares only first 5 characters
$result = strncasecmp("ApplePie", "appleSauce", 5); // Case-insensitive comparison of first 5 characters
This program compares strings using different functions and prints what it finds. It shows case-sensitive and case-insensitive comparisons, and compares full strings or just the first few characters.
<?php // Compare two strings case-sensitive $result1 = strcmp("hello", "Hello"); // Compare two strings ignoring case $result2 = strcasecmp("hello", "Hello"); // Compare first 3 characters case-sensitive $result3 = strncmp("hello", "helium", 3); // Compare first 3 characters ignoring case $result4 = strncasecmp("Hello", "heLium", 3); // Print results if ($result1 === 0) { echo "strcmp: Strings are equal\n"; } elseif ($result1 < 0) { echo "strcmp: 'hello' is less than 'Hello'\n"; } else { echo "strcmp: 'hello' is greater than 'Hello'\n"; } if ($result2 === 0) { echo "strcasecmp: Strings are equal ignoring case\n"; } if ($result3 === 0) { echo "strncmp: First 3 characters are equal\n"; } else { echo "strncmp: First 3 characters are different\n"; } if ($result4 === 0) { echo "strncasecmp: First 3 characters are equal ignoring case\n"; } else { echo "strncasecmp: First 3 characters are different ignoring case\n"; }
Remember that strcmp and strncmp are case-sensitive.
Use strcasecmp and strncasecmp when you want to ignore letter case.
These functions return an integer, not a boolean true/false.
String comparison functions check if texts are equal or which comes first.
Use strcmp and strncmp for case-sensitive checks.
Use strcasecmp and strncasecmp to ignore case differences.
Practice
Solution
Step 1: Understand case sensitivity in string comparison
strcmpcompares two strings exactly, including case.Step 2: Identify function behavior
strcmpreturns 0 if strings are exactly equal, considering case.Final Answer:
strcmp -> Option DQuick Check:
Case-sensitive compare = strcmp [OK]
- Confusing strcmp with strcasecmp (case-insensitive)
- Using strncasecmp for full string compare
- Thinking strcmp ignores case
Solution
Step 1: Identify function for case-insensitive partial compare
strncasecmpcompares first N characters ignoring case.Step 2: Check syntax correctness
strncasecmp($str1, $str2, 3)correctly compares first 3 chars case-insensitively.Final Answer:
strncasecmp($str1, $str2, 3) -> Option AQuick Check:
Partial case-insensitive compare = strncasecmp [OK]
- Using strncmp which is case-sensitive
- Passing length to strcasecmp (no length param)
- Using strcmp with length parameter
$result = strcasecmp('Hello', 'hello');
echo $result;Solution
Step 1: Understand strcasecmp behavior
It compares strings ignoring case and returns 0 if equal.Step 2: Compare 'Hello' and 'hello'
Ignoring case, both strings are equal, so result is 0.Final Answer:
0 -> Option BQuick Check:
strcasecmp('Hello', 'hello') = 0 [OK]
- Expecting 1 or -1 for equal strings
- Confusing case-sensitive strcmp with strcasecmp
- Thinking it throws error for case differences
if (strncmp('apple', 'Apple', 5) == 0) {
echo 'Match';
} else {
echo 'No match';
}Solution
Step 1: Analyze strncmp behavior
strncmpis case-sensitive, so 'apple' != 'Apple'.Step 2: Fix for case-insensitive compare
Usestrncasecmpto ignore case in first 5 chars.Final Answer:
Use strncasecmp instead of strncmp for case-insensitive compare -> Option CQuick Check:
Case-insensitive partial compare needs strncasecmp [OK]
- Assuming strncmp ignores case
- Using strcasecmp with length parameter
- Changing length unnecessarily
usort?Solution
Step 1: Understand sorting with case-insensitivity
Sorting ignoring case requires a case-insensitive comparison function.Step 2: Choose correct function for full string compare ignoring case
strcasecmpcompares full strings ignoring case, ideal for sorting.Final Answer:
strcasecmp -> Option AQuick Check:
Case-insensitive full compare for sorting = strcasecmp [OK]
- Using strcmp which is case-sensitive
- Using strncmp or strncasecmp which compare partial strings
- Passing no callback to usort
Ready to prove it?
Pass the test and this pattern moves unknown
