How can the substr function in PHP be utilized to compare strings of varying lengths?
When comparing strings of varying lengths in PHP, the substr function can be utilized to extract a portion of each string for comparison. By using substr to extract the same number of characters from each string, you can then compare these extracted substrings using standard string comparison functions like strcmp.
$string1 = "Hello, world!";
$string2 = "Hello!";
$length = min(strlen($string1), strlen($string2));
$substring1 = substr($string1, 0, $length);
$substring2 = substr($string2, 0, $length);
if(strcmp($substring1, $substring2) === 0){
echo "The substrings are equal.";
} else {
echo "The substrings are not equal.";
}