<?php $str1 = "Hello"; $str2 = "hello"; $result = strcmp($str1, $str2); echo $result; ?>
The strcmp function compares two strings in a case-sensitive way. Since uppercase 'H' has a lower ASCII value (72) than lowercase 'h' (104), the result is 72 - 104 = -32.
<?php $str1 = "World"; $str2 = "world"; $result = strcasecmp($str1, $str2); echo $result; ?>
The strcasecmp function compares two strings ignoring case. Since "World" and "world" differ only in case, the result is 0, meaning they are equal.
<?php $str1 = "apple"; $str2 = "apples"; $result = strncmp($str1, $str2, 5); echo $result; ?>
The strncmp function compares only the first 5 characters: "apple" and "apple" (from "apples"), which are identical, so the result is 0.
<?php $str1 = "abcabc"; $str2 = "xyzabc"; $result = substr_compare($str1, $str2, 3, 3); echo $result; ?>
The substrings compared are from offset 3 with length 3: "abc" from $str1 and "abc" from $str2 (starting at position 3).
Comparing 'a' (ASCII 97) and 'a' (ASCII 97), the difference is 0.
strncasecmp performs case-insensitive comparison up to a specified length (but starts from the beginning of strings). For offset, substr() can be used first. substr_compare supports offset/length but is case-sensitive unless the optional case-insensitivity flag is set. Among options, strncasecmp fits case-insensitive partial comparison best.